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.
Examples:
Input: str = “aa”, str2 = “ab”
Output:
abaa
aaab
aabaInput: str1 = “ab”, str2 = “de”
Output:
deab
daeb
adeb
abde
dabe
adbe
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Set to store strings // and avoid duplicates set<string> stringSet; // Recursive function to generate the required strings void find_permutation(string& str1, string& str2, int len1, int len2, int i, int j, string res) { // If current string is part of the result if (res.length() == len1 + len2) { // Insert it into the set stringSet.insert(res); return; } // If character from str1 can be chosen if (i < len1) find_permutation(str1, str2, len1, len2, i + 1, j, res + str1[i]); // If character from str2 can be chosen if (j < len2) find_permutation(str1, str2, len1, len2, i, j + 1, res + str2[j]); } // Function to print the generated // strings from the set void print_set() { set<string>::iterator itr; for (itr = stringSet.begin(); itr != stringSet.end(); itr++) cout << (*itr) << endl; } // Driver code int main() { string str1 = "aa", str2 = "ab"; int len1 = str1.length(); int len2 = str2.length(); find_permutation(str1, str2, len1, len2, 0, 0, ""); print_set(); return 0; } |
Java
// Java implementation of the approach import java.util.HashSet; class GFG { // Set to store strings // and avoid duplicates static HashSet<String> stringSet = new HashSet<>(); // Recursive function to generate the required strings public static void find_permutation(String str1, String str2, int len1, int len2, int i, int j, String res) { // If current string is part of the result if (res.length() == len1 + len2) { // Insert it into the set stringSet.add(res); return; } // If character from str1 can be chosen if (i < len1) find_permutation(str1, str2, len1, len2, i + 1, j, res + str1.charAt(i)); // If character from str2 can be chosen if (j < len2) find_permutation(str1, str2, len1, len2, i, j + 1, res + str2.charAt(j)); } // Function to print the generated // strings from the set public static void print_set() { for (String s : stringSet) System.out.println(s); } // Driver code public static void main(String[] args) { String str1 = "aa", str2 = "ab"; int len1 = str1.length(); int len2 = str2.length(); find_permutation(str1, str2, len1, len2, 0, 0, ""); print_set(); } } // This code is contributed by // sanjeev2552 |
Python3
# Python3 implementation of the approach # Set to store strings # and aduplicates stringSet=dict() # Recursive function to generate the required strings def find_permutation( str1,str2,len1,len2,i,j,res): # If currentis part of the result if (len(res) == len1 + len2): # Insert it into the set stringSet[res]=1 return # If character from str1 can be chosen if (i < len1): find_permutation(str1, str2, len1, len2,i + 1, j, res + str1[i]) # If character from str2 can be chosen if (j < len2): find_permutation(str1, str2, len1, len2,i, j + 1, res + str2[j]) # Function to print the generated # strings from the set def print_set(): for i in stringSet: print(i) # Driver code str1 = "aa"str2 = "ab"len1 = len(str1) len2 = len(str2) find_permutation(str1, str2, len1,len2, 0, 0, "") print_set() # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Set to store strings // and avoid duplicates static HashSet<String> stringSet = new HashSet<String>(); // Recursive function to generate the required strings public static void find_permutation(String str1, String str2, int len1, int len2, int i, int j, String res) { // If current string is part of the result if (res.Length == len1 + len2) { // Insert it into the set stringSet.Add(res); return; } // If character from str1 can be chosen if (i < len1) find_permutation(str1, str2, len1, len2, i + 1, j, res + str1[i]); // If character from str2 can be chosen if (j < len2) find_permutation(str1, str2, len1, len2, i, j + 1, res + str2[j]); } // Function to print the generated // strings from the set public static void print_set() { foreach (String s in stringSet) Console.WriteLine(s); } // Driver code public static void Main(String[] args) { String str1 = "aa", str2 = "ab"; int len1 = str1.Length; int len2 = str2.Length; find_permutation(str1, str2, len1, len2, 0, 0, ""); print_set(); } } // This code is contributed by 29AjayKumar |
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.
Recommended Posts:
- 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
- Generate all distinct subsequences of array using backtracking
- 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
- Find distinct characters in distinct substrings of a string
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- 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
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Count of 'GFG' Subsequences in the given string
- Lexicographically smallest permutation of a string with given subsequences
- Number of subsequences in a given binary string divisible by 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

