Generate all possible strings such that char at index i is either str1 [ i ] or str2 [ i ] | C ++ implementation of the approach ; Recursive function to generate the required strings ; If length of the current string is equal to the length of the given strings then the current string is part of the result ; Choosing the current character from the string a ; Choosing the current character from the string b ; Driver code ; Third argument is an empty string that we will be appended in the recursion calls Fourth arguments is the length of the resultant string so far
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateStr ( char * a , char * b , string s , int count , int len ) { if ( count == len ) { cout << s << endl ; return ; } generateStr ( a + 1 , b + 1 , s + ( * a ) , count + 1 , len ) ; generateStr ( a + 1 , b + 1 , s + ( * b ) , count + 1 , len ) ; } int main ( ) { char * a = " abc " , * b = " def " ; int n = strlen ( a ) ; generateStr ( a , b , " " , 0 , n ) ; return 0 ; }