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.

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

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

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

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :
Practice Tags :