Count the Number of matching characters in a pair of strings - GeeksforGeeks

21 May, 2021
Given a pair of non-empty strings str1 and str2, the task is to count the number of matching characters in these strings. Consider the single count for the character which have duplicates in the strings.Examples:  
Input: str1 = “abcdef”, str2 = “defghia” Output: 4 Matching characters are: a, d, e, fInput: str1 = “aabcddekll12”, str2 = “bb22ll@55k” Output: 5 Matching characters are: b, 1, 2, @, k  
 
Approach:  
Initialize a counter variable with 0. Iterate over the first string from the starting character to ending character. If the character extracted from the first string is found in the second string, then increment the value of the counter by 1.The final answer will be count/2 as the duplicates are not being considered.Output the value of counter
Initialize a counter variable with 0. 
Iterate over the first string from the starting character to ending character. 
If the character extracted from the first string is found in the second string, then increment the value of the counter by 1.
The final answer will be count/2 as the duplicates are not being considered.
Output the value of counter
Below is the implementation of the above approach.  
C++
Java
Python3
C#
Javascript
// C++ code to count number of matching// characters in a pair of strings #include <bits/stdc++.h>using namespace std; // Function to count the matching charactersvoid count(string str1, string str2){    int c = 0, j = 0;     // Traverse the string 1 char by char    for (int i = 0; i < str1.length(); i++) {         // This will check if str1[i]        // is present in str2 or not        // str2.find(str1[i]) returns -1 if not found        // otherwise it returns the starting occurrence        // index of that character in str2        if (str2.find(str1[i]) >= 0            and j == str1.find(str1[i]))            c += 1;        j += 1;    }    cout << "No. of matching characters are: "         << c / 2;} // Driver codeint main(){    string str1 = "aabcddekll12@";    string str2 = "bb2211@55k";     count(str1, str2);}
// Java code to count number of matching// characters in a pair of stringsclass GFG{         // Function to count the matching characters    static void count(String str1, String str2)    {        int c = 0, j = 0;             // Traverse the string 1 char by char        for (int i = 0; i < str1.length(); i++)        {                 // This will check if str1[i]            // is present in str2 or not            // str2.find(str1[i]) returns -1 if not found            // otherwise it returns the starting occurrence            // index of that character in str2            if (str2. indexOf(str1.charAt(i)) >= 0)            {                c += 1;        }    }        System.out.println("No. of matching characters are: " + c);    }         // Driver code    public static void main (String[] args)    {        String str1 = "aabcddekll12@";        String str2 = "bb2211@55k";             count(str1, str2);    }} // This code is contributed by AnkitRai01
# Python3 code to count number of matching# characters in a pair of strings # Function to count the matching charactersdef count(str1, str2) :     c = 0; j = 0;     # Traverse the string 1 char by char    for i in range(len(str1)) :         # This will check if str1[i]        # is present in str2 or not        # str2.find(str1[i]) returns -1 if not found        # otherwise it returns the starting occurrence        # index of that character in str2        if str1[i] in str2 :            c += 1;            #print(str1[i])        j += 1;         print("No. of matching characters are: ", c );  # Driver codeif __name__ == "__main__" :    str1 = "aabcddekll12@";    str2 = "bb2211@55k";         count(str1, str2);     # This code is contributed by AnkitRai01
// C# code to count number of matching// characters in a pair of stringsusing System; class GFG{         // Function to count the matching characters    static void count(string str1, string str2)    {        int c = 0, j = 0;             // Traverse the string 1 char by char        for (int i = 0; i < str1.Length; i++)        {                 // This will check if str1[i]            // is present in str2 or not            // str2.find(str1[i]) returns -1 if not found            // otherwise it returns the starting occurrence            // index of that character in str2            if (str2.IndexOf(str1[i]) >= 0)            {                c += 1;        }    }        Console.WriteLine("No. of matching characters are: " + c);    }         // Driver code    public static void Main()    {        string str1 = "aabcddekll12@";        string str2 = "bb2211@55k";             count(str1, str2);    }} // This code is contributed by AnkitRai01
<script> // JavaScript code to count number of matching// characters in a pair of strings // Function to count the matching charactersfunction count(str1, str2){    var c = 0;     // Traverse the string 1 char by char    for (var i = 0; i < str1.length; i++) {         // This will check if str1[i]        // is present in str2 or not        // str2.find(str1[i]) returns -1 if not found        // otherwise it returns the starting occurrence        // index of that character in str2        if (str2.includes(str1[i]))            c += 1;    }    document.write( "No. of matching characters are: " +         + parseInt(c));} // Driver code var str1 = "aabcddekll12@";var str2 = "bb2211@55k";count(str1, str2);  </script>
No. of matching characters are: 5
 
ankthon
importantly
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
sprintf() in C
String class in Java | Set 1
Convert character array to string in C++
Print all subsequences of a string
Program to count occurrence of a given character in a string
How to Append a Character to a String in C
Naive algorithm for Pattern Searching