Distinct strings such that they contains given strings as sub | Java 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
import java . util . HashSet ; class GFG { static HashSet < String > stringSet = new HashSet < > ( ) ; public static void find_permutation ( String str1 , String str2 , int len1 , int len2 , int i , int j , String res ) { if ( res . length ( ) == len1 + len2 ) { stringSet . add ( res ) ; return ; } if ( i < len1 ) find_permutation ( str1 , str2 , len1 , len2 , i + 1 , j , res + str1 . charAt ( i ) ) ; if ( j < len2 ) find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 . charAt ( j ) ) ; } public static void print_set ( ) { for ( String s : stringSet ) System . out . println ( s ) ; } 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 ( ) ; } }