Given two strings s1 and s2 consisting of lowercase English alphabets, the task is to count all the pairs of indices (i, j) from the given strings such that s1[i] = s2[j] and all the indices are distinct i.e. if s1[i] pairs with some s2[j] then these two characters will not be paired with any other character. Example
Input: s1 = “abcd”, s2 = “aad” Output: 2 (s1, s2) and (s1, s2) are the only valid pairs. (s1, s2) is not includes because s1 has already been paired with s2 Input: s1 = “geeksforgeeks”, s2 = “platformforgeeks” Output: 8
Approach: Count the frequencies of all the characters from both strings. Now, for every character if the frequency of this character in string s1 is freq1 and in string s2 is freq2 then total valid pairs with this character will be min(freq1, freq2). The sum of this value for all the characters is the required answer. Below is the implementation of the above approach:
Please Login to comment...