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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Generating distinct subsequences of a given string in lexicographic order
- Count subsequences which contains both the maximum and minimum array element
- Count Distinct Subsequences
- Count of subsequences having maximum distinct elements
- Count of subsequences of length atmost K containing distinct prime elements
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Find distinct characters in distinct substrings of a string
- Convert a number of length N such that it contains any one digit at least 'K' times
- Modify the string such that it contains all vowels at least once
- Minimum K such that every substring of length atleast K contains a character c
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum flips required in a binary string such that all K-size substring contains 1
- Find all distinct palindromic sub-strings of a given string
- Check if given string can be split into four distinct strings
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Find the number of strings formed using distinct characters of a given string
- Count of distinct possible strings after performing given operations
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
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.