How to generate all possible binary strings by replacing ? from a given pattern in Node.js

1 Answer

0 votes
function generate_all_possible_binary_strings(str) {
    let q = [];
    q.push(str);
 
    while (q.length > 0) {
        let temp = q[0];

        let index = temp.indexOf('?');
     
        if (index != -1) {
            temp = temp.replace(temp[index] , '0');
            q.push(temp);

            temp = temp.replace(temp[index] , '1');
            q.push(temp);
        }
 
        else {
            console.log(temp);
        }
 
        q.shift();
    }
}
 
let str = "1?0?1";

generate_all_possible_binary_strings(str);





/*
run:

10001
11001
11001
11101

*/

 



answered Aug 24, 2023 by avibootz
...