Distinct strings such that they contains given strings as sub | 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
let stringSet = new Set ( ) ; function find_permutation ( str1 , str2 , len1 , len2 , i , j , res ) { if ( res . length == len1 + len2 ) { stringSet . add ( 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 ] ) ; } function print_set ( ) { for ( let s of stringSet . values ( ) ) { document . write ( s + " " ) ; } } let str1 = " " , str2 = " " ; let len1 = str1 . length ; let len2 = str2 . length ; find_permutation ( str1 , str2 , len1 , len2 , 0 , 0 , " " ) ; print_set ( ) ;