Distinct strings such that they contains given strings as sub | C ++ implementation of the approach ; Set to store strings and avoid duplicates ; Recursive function to generate the required strings ; If current string is part of the result ; Insert it into the set ; If character from str1 can be chosen ; If character from str2 can be chosen ; Function to print the generated strings from the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < string > stringSet ; void find_permutation ( string & str1 , string & str2 , int len1 , int len2 , int i , int j , string res ) { if ( res . length ( ) == len1 + len2 ) { stringSet . insert ( res ) ; return ; } if ( i < len1 ) find_permutation ( str1 , str2 , len1 , len2 , i + 1 , j , res + str1 [ i ] ) ; if ( j < len2 ) find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 [ j ] ) ; } void print_set ( ) { set < string > :: iterator itr ; for ( itr = stringSet . begin ( ) ; itr != stringSet . end ( ) ; itr ++ ) cout << ( * itr ) << endl ; } 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 ; }