Count the Number of matching characters in a pair of strings

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, f

Input: str1 = “aabcddekll12”, str2 = “bb22ll@55k”
Output: 5
Matching characters are: b, 1, 2, @, k

Approach:



  1. Initialize a counter variable with 0.
  2. Iterate over the first string from the starting character to ending character.
  3. If the character extracted from the first string is found in the second string, then increment the value of the counter by 1.
  4. The final answer will be count/2 as the duplicates are not being considered.
  5. Output the value of counter

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 characters
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.find(str1[i]) >= 0
            and j == str1.find(str1[i]))
            c += 1;
        j += 1;
    }
    cout << "No. of matching characters are: "
         << c / 2;
}
  
// Driver code
int main()
{
    string str1 = "aabcddekll12@";
    string str2 = "bb2211@55k";
  
    count(str1, str2);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to count number of matching 
// characters in a pair of strings 
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.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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to count number of matching 
# characters in a pair of strings 
  
# Function to count the matching characters 
def 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 code 
if __name__ == "__main__" :
    str1 = "aabcddekll12@";
    str2 = "bb2211@55k";
      
    count(str1, str2); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to count number of matching 
// characters in a pair of strings 
using 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

chevron_right


Output:

No. of matching characters are: 5

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : AnkitRai01

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.