Skip to content
Related Articles

Related Articles

Improve Article
Count the Number of matching characters in a pair of strings
  • Difficulty Level : Basic
  • Last Updated : 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:
Matching characters are: a, d, e, f
Input: str1 = “aabcddekll12”, str2 = “bb22ll@55k” 
Output:
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++




// 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);
}

Java




// 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

Python3




# 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

C#




// 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

Javascript




<script>
 
// JavaScript code to count number of matching
// characters in a pair of strings
 
// Function to count the matching characters
function 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>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :