Table of Contents
Introduction
Our task is to find the uniquely matched characters from the two given string.
Program
Approach 1:
def char_count(x, y): x = x.lower() y = y.lower() str1 = set(x) str2 = set(y) comn_char = str1 & str2 print("Total numbers of characters matched are : "+str(len(comn_char))) input_str1 = input("Enter the first string : ") input_str2 = input("Enter the second string : ") char_count(input_str1 , input_str2)
Output:
Approach 2:
Output:
Explanation
In the above python code, we have used two approaches to uniquely identify the common characters in the given two strings. In approach 1, we first converted both the strings in lower case and then applied set() to the strings to remove the duplicate characters. To find the common characters between the two strings we applied bitwise AND ( & ) on the strings. And to find the character count we used len() function and printed the output on the screen.
In approach 2, we have created an empty list and checked each character of string 1 whether it is present in the list. If the character is present in the list then the value is passed and next char is checked (This is used to remove the duplicates from our result). If the character is not found in the list, it is searched in string 2 using re.search(). And if found it is appended to the list and count is increased by 1. When all the elements are traversed, the output count is printed on screen.
0 Comments