// 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;
}