Generate all binary strings from given pattern | Recursive C ++ program to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; No need to backtrack as string is passed by value to the function ; Driver code to test above function
#include <iostream> NEW_LINE using namespace std ; void print ( string str , int index ) { if ( index == str . size ( ) ) { cout << str << endl ; return ; } if ( str [ index ] == ' ? ' ) { str [ index ] = '0' ; print ( str , index + 1 ) ; str [ index ] = '1' ; print ( str , index + 1 ) ; } else print ( str , index + 1 ) ; } int main ( ) { string str = "1 ? ? 0?101" ; print ( str , 0 ) ; return 0 ; }