How to generate all possible binary strings by replacing ? from a given pattern in TypeScript

1 Answer

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

        let index: number = 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: string = "1?0?1";

generate_all_possible_binary_strings(str);





/*
run:

"10001"
"11001"
"11001"
"11101"

*/
    

 



answered Aug 24, 2023 by avibootz
...