Generate all binary strings from given pattern
Given a string containing of ‘0’, ‘1’ and ‘?’ wildcard characters, generate all binary strings that can be formed by replacing each wildcard character by ‘0’ or ‘1’.
Example :
Input: str = “1??0?101”
Output:
10000101
10001101
10100101
10101101
11000101
11001101
11100101
11101101
Method 1 (Using Recursion)
We pass index of next character to the recursive function. If the current character is a wildcard character ‘?’, we replace it with ‘0’ or ‘1’ and do the same for all remaining characters. We print the string if we reach at its end.
Algorithm:
Step 1: Initialize the string first with some wildcard characters in it.
Step 2: Check if index position is equals to the size of string, If it is return.
Step 3: If wildcard character is present at index location, replace it by 0 or 1 accordingly.
Step 4: Print the output
Below is the implementation of the above approach:
C++
#include <iostream>
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;
}
|
Java
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 );
}
}
|
Python3
def _print(string, index):
if index = = len (string):
print (''.join(string))
return
if string[index] = = "?" :
string[index] = '0'
_print(string, index + 1 )
string[index] = '1'
_print(string, index + 1 )
string[index] = '?'
else :
_print(string, index + 1 )
if __name__ = = "__main__" :
string = "1??0?101"
string = list (string)
_print(string, 0 )
|
C#
using System;
class GFG
{
public static void print( char []str,
int index)
{
if (index == str.Length)
{
Console.WriteLine(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 input = "1??0?101" ;
char []str = input.ToCharArray();
print(str, 0);
}
}
|
PHP
<?php
function print1( $str , $index )
{
if ( $index == strlen ( $str ))
{
echo $str . "\n" ;
return ;
}
if ( $str [ $index ] == '?' )
{
$str [ $index ] = '0' ;
print1( $str , $index + 1);
$str [ $index ] = '1' ;
print1( $str , $index + 1);
}
else
print1( $str , $index + 1);
}
$str = "1??0?101" ;
print1( $str , 0);
?>
|
Javascript
<script>
function print(str, index) {
if (index === str.length) {
document.write(str.join( "" ) + "<br>" );
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);
}
var input = "1??0?101" ;
var str = input.split( "" );
print(str, 0);
</script>
|
Output:
10000101
10001101
10100101
10101101
11000101
11001101
11100101
11101101
Time Complexity: O(2N), where N is the length of the given string and there are 2 possibilities.
Auxiliary Space: O(N2), as a copy of the string is created in every recursive call.
Method 2 (Using Queue)
We can also achieve this by using iteration. The idea is to use queue, We find position of first occurrence of wildcard character in the input string and replace it by ‘0’ , then ‘1’ and push both strings into the queue. Then we pop next string from the queue, and repeat the process till queue is empty. If no wildcard characters are left, we simply print the string.
Iterative C++ implementation using queue.
C++
#include <iostream>
#include <queue>
using namespace std;
void print(string str)
{
queue<string> q;
q.push(str);
while (!q.empty())
{
string str = q.front();
size_t index = str.find( '?' );
if (index != string::npos)
{
str[index] = '0' ;
q.push(str);
str[index] = '1' ;
q.push(str);
}
else
cout << str << endl;
q.pop();
}
}
int main()
{
string str = "1??0?101" ;
print(str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void print(String str)
{
Queue<String> q = new LinkedList<>();
q.add(str);
while (!q.isEmpty())
{
str = q.remove();
int index = str.indexOf( '?' );
if (index != - 1 )
{
str = str.substring( 0 ,index) + '0' + str.substring(index+ 1 );
q.add(str);
str = str.substring( 0 ,index) + '1' + str.substring(index+ 1 );
q.add(str);
}
else
System.out.println(str);
}
}
public static void main(String args[])
{
String str = "1??0?101" ;
print(str);
}
}
|
Python3
def Print ( Str ):
q = []
q.append( Str )
while ( len (q) > 0 ):
Str = q[ 0 ]
try :
index = Str .index( '?' )
except ValueError:
index = - 1
if (index ! = - 1 ):
s1 = Str .replace( '?' , '0' , 1 )
q.append(s1)
s2 = Str .replace( '?' , '1' , 1 )
q.append(s2)
else :
print ( Str )
q.pop( 0 )
Str = "1??0?101"
Print ( Str )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void print( string Str)
{
var q = new List< string >();
q.Add(Str);
while (q.Count > 0) {
Str = q[0];
int index = Str.IndexOf( '?' );
if (index != -1) {
Str = Str.Substring(0, index) + '0'
+ Str.Substring(index + 1);
q.Add(Str);
Str = Str.Substring(0, index) + '1'
+ Str.Substring(index + 1);
q.Add(Str);
}
else {
Console.WriteLine(Str);
}
q.RemoveAt(0);
}
}
public static void Main( string [] args)
{
string str = "1??0?101" ;
print(str);
}
}
|
Javascript
<script>
function Print(Str){
let q = []
q.push(Str)
while (q.length > 0){
let Str = q[0]
let index = Str.indexOf( '?' )
if (index != -1)
{
Str = Str.replace(Str[index] , '0' )
q.push(Str)
Str = Str.replace(Str[index] , '1' )
q.push(Str)
}
else
{
document.write(Str, "</br>" )
}
q.shift()
}
}
let Str = "1??0?101"
Print(Str)
</script>
|
Output:
10000101
10001101
10100101
10101101
11000101
11001101
11100101
11101101
Time Complexity: O(N*2N), where N is the size of the string.
Auxiliary Space: O(2N)
Method 3 (Using str and Recursion)
C++
#include <bits/stdc++.h>
using namespace std;
vector<string> res;
void genBin(string s)
{
auto pos = s.find( '?' );
if (pos != string::npos) {
string s1 = s;
s1.replace(pos, 1, "0" );
string s2 = s;
s2.replace(pos, 1, "1" );
genBin(s1);
genBin(s2);
}
else {
res.push_back(s);
}
}
int main()
{
genBin( "1??0?101" );
for (string x : res) {
cout << x << " " ;
}
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static ArrayList<String>res = new ArrayList<String>();
static void genBin(String s) {
if (s.indexOf( '?' ) != - 1 ) {
String s1 = s.replaceFirst( "\\?" , "0" );
String s2 = s.replaceFirst( "\\?" , "1" );
genBin(s1);
genBin(s2);
}
else res.add(s);
}
public static void main(String[] args) {
genBin( "1??0?101" );
System.out.println(res);
}
}
|
Python3
res = []
def genBin(s):
if '?' in s:
s1 = s.replace( '?' , '0' , 1 )
s2 = s.replace( '?' , '1' , 1 )
genBin(s1)
genBin(s2)
else : res.append(s)
genBin( "1??0?101" )
print (res)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List< string > res = new List< string >();
static void genBin( string s)
{
int ind = s.IndexOf( "?" );
if (ind != -1) {
string s1 = s.Remove(ind, 1).Insert(
ind, "0" );
string s2 = s.Remove(ind, 1).Insert(
ind, "1" );
genBin(s1);
genBin(s2);
}
else
res.Add(s);
}
public static void Main( string [] args)
{
genBin( "1??0?101" );
foreach ( var ele in res) Console.Write(ele + " " );
}
}
|
Javascript
<script>
let res = []
function genBin(s){
if (s.includes( '?' )){
let s1 = s.replace(/\?/, '0' )
let s2 = s.replace(/\?/, '1' )
genBin(s1)
genBin(s2)
} else {
res.push(s)
}
}
genBin( "1??0?101" )
document.write(res)
</script>
|
Output:
['10000101', '10001101', '10100101', '10101101', '11000101', '11001101', '11100101', '11101101']
Time Complexity: O(N*2N), where N is the size of the string.
Auxiliary Space: O(2N)
Last Updated :
20 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...