Distinct strings such that they contains given strings as sub | Set to store strings and aduplicates ; Recursive function to generate the required strings ; If currentis 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
stringSet = dict ( ) NEW_LINE def find_permutation ( str1 , str2 , len1 , len2 , i , j , res ) : NEW_LINE INDENT if ( len ( res ) == len1 + len2 ) : NEW_LINE INDENT stringSet [ res ] = 1 NEW_LINE return NEW_LINE DEDENT if ( i < len1 ) : NEW_LINE INDENT find_permutation ( str1 , str2 , len1 , len2 , i + 1 , j , res + str1 [ i ] ) NEW_LINE DEDENT if ( j < len2 ) : NEW_LINE INDENT find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 [ j ] ) NEW_LINE DEDENT DEDENT def print_set ( ) : NEW_LINE INDENT for i in stringSet : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT str1 = " aa " NEW_LINE str2 = " ab " NEW_LINE len1 = len ( str1 ) NEW_LINE len2 = len ( str2 ) NEW_LINE find_permutation ( str1 , str2 , len1 , len2 , 0 , 0 , " " ) NEW_LINE print_set ( ) NEW_LINE