Check if there is any common character in two given strings

Given two strings. The task is to check that is there any common character in between two strings.

Examples:

Input: s1 = "geeksforgeeks", s2 = "geeks"
Output: Yes

Input: s1 = "geeks", s2 = "for"
Output: No


Approach: Traverse the 1st string and map the characters of the string with its frequency, in this map characters act as a key and the frequency its value. Then traverse the second string and we will check if there is any character that is present in both the string then it is confirmed that there is a common sub-sequence.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implemenation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to match character
bool check(string s1, string s2)
{
    // Create a map to map
    // characters of 1st string
    map<char, int> map;
  
    // traverse the first string
    // and create a hash map
    for (int i = 0; i < s1.length(); i++)
        map[s1[i]]++;
  
    // traverse the second string
    // and if there is any
    // common character than return 1
    for (int i = 0; i < s2.length(); i++)
        if (map[s2[i]] > 0)
            return true;
  
    // else return 0
    return false;
}
  
// Driver code
int main()
{
    // Declare two strings
    string s1 = "geeksforgeeks", s2 = "geeks";
  
    // Find if there is a common subsequence
    bool yes_or_no = check(s1, s2);
  
    if (yes_or_no == true)
        cout << "Yes" << endl;
  
    else
        cout << "No" << endl;
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check whether 
# two lists are overlaping or not
def is_member(List, key):
  
    for i in range(0, len(List)):
        if key == List[i]:
            return True
    return False
  
def overlap(List1 , List2):
  
    for key in List1:
        if is_member( List2, key ):
            return True
  
    return False
  
# Driver Code
if __name__ == '__main__':
  
    s1 = 'geeksforgeeks'
    s2 = 'geeks'
  
    List1 = list( s1 )
    List2 = list( s2 )
  
    yes_or_no = str(overlap( List1, List2 ))
      
    if (yes_or_no):
        print("Yes")
    else:
        print("No")
  
# This code is contributed 
# by Krishna_Yadav

chevron_right


Output:

Yes

Time Complexity: O(n) where n is the length of the string



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Krishna_Yadav



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.