Generate all binary strings from given pattern | Recursive Java 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 ; NOTE : Need to backtrack as string is passed by reference to the function ; driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class binStr { public static void print ( char str [ ] , int index ) { if ( index == str . length ) { System . out . println ( str ) ; return ; } if ( str [ index ] == ' ? ' ) { str [ index ] = '0' ; print ( str , index + 1 ) ; str [ index ] = '1' ; print ( str , index + 1 ) ; str [ index ] = ' ? ' ; } else print ( str , index + 1 ) ; } public static void main ( String [ ] args ) { String input = "1 ? ? 0?101" ; char [ ] str = input . toCharArray ( ) ; print ( str , 0 ) ; } }