Open In App

Generate all binary strings from given pattern

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
26 Likes
Like
Report

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++
// Recursive C++ program to generate all binary strings
// formed by replacing each wildcard character by 0 or 1
#include <iostream>
using namespace std;

// Recursive function to generate all binary strings
// formed by replacing each wildcard character by 0 or 1
void print(string str, int index)
{
    if (index == str.size())
    {
        cout << str << endl;
        return;
    }

    if (str[index] == '?')
    {
        // replace '?' by '0' and recurse
        str[index] = '0';
        print(str, index + 1);

        // replace '?' by '1' and recurse
        str[index] = '1';
        print(str, index + 1);

        // No need to backtrack as string is passed
        // by value to the function
    }
    else
        print(str, index + 1);
}

// Driver code to test above function
int main()
{
    string str = "1??0?101";

    print(str, 0);

    return 0;
}
Java Python3 C# PHP JavaScript

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++
// Iterative C++ program to generate all binary
// strings formed by replacing each wildcard
// character by 0 or 1
#include <iostream>
#include <queue>
using namespace std;

// Iterative function to generate all binary strings
// formed by replacing each wildcard character by 0
// or 1
void print(string str)
{
    queue<string> q;
    q.push(str);

    while (!q.empty())
    {
        string str = q.front();

        // find position of first occurrence of wildcard
        size_t index = str.find('?');

        // If no matches were found,
        // find returns string::npos
        if(index != string::npos)
        {
            // replace '?' by '0' and push string into queue
            str[index] = '0';
            q.push(str);

            // replace '?' by '1' and push string into queue
            str[index] = '1';
            q.push(str);
        }

        else
            // If no wildcard characters are left,
            // print the string.
            cout << str << endl;

        q.pop();
    }
}

// Driver code to test above function
int main()
{
    string str = "1??0?101";

    print(str);

    return 0;
}
Java Python3 C# JavaScript

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++
// C++ program to implement the approach
#include <bits/stdc++.h>

using namespace std;

/* we store processed strings in all (array)
we see if string as "?", if so, replace it with 0 and 1
and send it back to recursive func until base case is
reached which is no wildcard left */

vector<string> res;

void genBin(string s)
{
    auto pos = s.find('?');
    if (pos != string::npos) {
        // copying s to s1
        string s1 = s;
        // replacing first occurrence of ?
        // with 0
        s1.replace(pos, 1, "0");
        // copying s to s2
        string s2 = s;
        // replacing first occurrence of ?
        // with 1
        s2.replace(pos, 1, "1");
        genBin(s1);
        genBin(s2);
    }
    else {
        res.push_back(s);
    }
}

// Driver code
int main()
{
    genBin("1??0?101");
    for (string x : res) {
        cout << x << " ";
    }
}

// This code is contributed by phasing17
Java Python3 C# JavaScript

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)


Article Tags :
Practice Tags :

Similar Reads