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.
Input: str1 = “abcdef”, str2 = “defghia”
Matching characters are: a, d, e, f
Input: str1 = “aabcddekll12”, str2 = “bb22ll@55k”
Matching characters are: b, 1, 2, @, k
- Initialize a counter variable with 0.
- Iterate over the first string from the starting character to ending character.
- If the character extracted from the first string is found in the second string, then increment the value of the counter by 1.
- The final answer will be count/2 as the duplicates are not being considered.
- Output the value of counter
Below is the implementation of the above approach.
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.
- Remove characters from the first string which are present in the second string
- A Program to check if strings are rotations of each other or not
- Print list items containing all characters of a given word
- Find the smallest window in a string containing all characters of another string
- Length of the longest substring without repeating characters
- Check whether two strings are anagram of each other
- Print all permutations with repetition of characters
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- Given a number, find the next smallest palindrome
- Count words in a given string
- String matching where one string contains wildcard characters
- Find if a string is interleaved of two other strings | DP-33
- Find the first non-repeating character from a stream of characters
- Rearrange a string so that all same characters become d distance away
- Find Excel column name from a given column number
- How to turn off a particular bit in a number?
- Given a sorted dictionary of an alien language, find order of characters
- Find if an array of strings can be chained to form a circle | Set 1
- Given two strings, find if first string is a subsequence of second
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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