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
using System ; using System . Collections . Generic ; class GFG { static HashSet < String > stringSet = new HashSet < String > ( ) ; 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 [ i ] ) ; if ( j < len2 ) find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 [ j ] ) ; } public static void print_set ( ) { foreach ( String s in stringSet ) Console . WriteLine ( 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 ( ) ; } }