Given two strings str1 and str2 of lengths M and N respectively. The task is to find all the distinct string of length M + N such that the frequency of any character in the resultant string is equal to the sum of frequencies of the same character in the given strings and both the given strings are present as a sub-sequence in all the generated strings.
Input: str = “aa”, str2 = “ab”
Input: str1 = “ab”, str2 = “de”
Approach: This problem can be solved using recursion. Set two pointers i and j to the beginnings of strings str1 and str2 respectively. Now, at every recursive call we have two choices to select either the character at str1[i] or the character at str2[j] and the termination condition will be when the length of the resultant string becomes equal to len(str1) + len(str2). Use an unordered_set in order to avoid duplicates.
Below is the implementation of the above approach:
aaab aaba abaa
- Distinct strings with odd and even changes allowed
- Check if given string can be split into four distinct strings
- Find all distinct palindromic sub-strings of a given string
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Distinct state codes that appear in a string as contiguous sub-strings
- Print all distinct circular strings of length M in lexicographical order
- Meta Strings (Check if two strings can become same after a swap in one string)
- Search in an array of strings where non-empty strings are sorted
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Pairs of complete strings in two sets of strings
- Count Distinct Subsequences
- Count of subsequences having maximum distinct elements
- Generating distinct subsequences of a given string in lexicographic order
- Add n binary strings
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.