Signup/Sign In

Python program to Count the Number of matching characters in a pair of string

In this tutorial, we will learn to count the number of matching characters in the given two strings. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. For a given two non-empty strings, we have to count the total number of matching characters in those strings. Duplicate characters will be counted only once. For example,

Input:

s1="abdcpqref"

s2= "abcdefgphi"

Output: 5

To solve this problem, there are several different approaches, some of them are:

  1. Using find() method
  2. Using a set

Approach 1: using find() function

In this approach, we will use the find() method of the String class to find characters of the first string in the second string. We will run a loop for the characters in string 1 and with the help of a counter variable, we will keep track of the matching characters.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function to count the matching characters

Step 2- Declare a counter variable

Step 3- Declare a variable for iterating over the second string

Step 4- Run a loop to iterate over string 1 and find the characters of string 1 in string 2

Step 5- When a matching character is found increase the counter variable

Step 6- Print the counter variable

Step 7- Initialise two strings

Step 8- Call the function

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. The function will accept both the strings and then count the matching characters using for loop and a counter variable.

def count(s1, s2):
    c=0 #counter variable
    j=0
    for i in s1:
        if s2.find(i)>-0 and j==s1.find(i):
            c=c+1
        j=j+1
    print("Matching char: ",c)

s1="aabcdefk12"
s2="b2acdefk1"
count(s1,s2)


Matching char: 8

Approach 2: Using a set in Python

In this approach, we will convert both the strings into the set and then find the common characters using the and (&) operator. This operator returns the intersection of both sets which is the value common to both sets.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will find the matching characters

Step 2- Declare a set for storing the strings in set format

Step 3- In another variable store the intersection set of set1 and set 2

Step 4- Print the length of the intersection set

Step 5- Initialise two strings

Step 6- Call the function

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. The intersection of two sets can be found by using the & operator. To find the length of the intersection set, we will use the len() method.

def count(s1, s2):
    char_of_s1=set(s1)
    char_of_s2=set(s2)    
    common_char= char_of_s1 & char_of_s2
    print("Matching Characters: ",len(common_char))
s1="abgfhij2@1"
s2="@1khgabop"
count(s1,s2)


Matching Characters: 6

Conclusion

In this approach, we have seen two approaches to count the matching characters in two non-empty strings. We have seen the approach of using the find() method to count matching characters and the set approach of finding the intersection between the two strings.