Open In App

JavaScript Program to Generate all Binary Strings Without Consecutive 1’s

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer, K. Generate all binary strings of size k without consecutive 1’s.

Examples:

Input : K = 3  
Output : 000 , 001 , 010 , 100 , 101
Input : K = 4
Output: 0000 0001 0010 0100 0101 1000 1001 1010

So, let’s see each of the approaches with its practical implementation.

Approach 1: Using Recursive Function

Here, the recursive function generates all the binary strings of the input size ‘K’ without consecutive 1’s by actually appending the ‘0’ and the ‘1’ to the input string ‘str’. In each step there, is the appending of 0 or 1, and the binary strings are stored in the res variable and printed using the log function.

Example: In this example, we will Generate all binary strings without consecutive 1’s in JavaScript using the Recursive Function.

Javascript




let K = 5;
let res = "";
  
function binaryStr(str, n) {
    if (n === K) {
        res += str + " ";
        return;
    }
    if (n === 0 || str[n - 1] === "0") {
        binaryStr(str + "0", n + 1);
        binaryStr(str + "1", n + 1);
    } else {
        binaryStr(str + "0", n + 1);
    }
}
  
// Strings starting with 0
binaryStr("0", 1);
// Strings starting with 1
binaryStr("1", 1);
  
console.log(res.trim());


Output

00000 00001 00010 00100 00101 01000 01001 01010 10000 10001 10010 10100 10101

Approach 2: Using Stack Data Structure

In this specified approach, we are using the Stack Data Structure where the empty stack is used initially and pushed, and the popping of the binary strings is done to make sure that there are no consecutive 1s in the string. Then we display the results in the descending sequence.

Example: In this example, we will Generate all binary strings without consecutive 1’s in JavaScript using the Stack Data Structure.

Javascript




function binaryStr(K) {
    let stack = [''];
    let res = [];
    while (stack.length > 0) {
        let str = stack.pop();
        if (str.length === K) {
            res.push(str);
            continue;
        }
        if (str[str.length - 1] === '1') {
            stack.push(str + '0');
        } else {
            stack.push(str + '0');
            stack.push(str + '1');
        }
    }
    console.log(res.reverse().join(' '));
}
binaryStr(6);


Output

000000 000001 000010 000100 000101 001000 001001 001010 010000 010001 010010 010100 010101 100000 100001 100010 100100 100101 101000 101001 101010

Approach 3: Using Iteration and padStart() Method

In this approach, we are using the builtin methods to generate all the binary strings by performing the loop and mathematical operations. We convert the decimal number to a binary string and make sure that there are no consecutive 1s included in the string. Then we print these strings using the log function.

Example: In this example, we will Generate all binary strings without consecutive 1’s in JavaScript using Math.pow(), toString(), padStart() and includes() Methods

Javascript




function binaryStr(K) {
    let results = [];
    for (let i = 0; i < Math.pow(2, K); i++) {
        let res = i.toString(2).padStart(K, "0");
        if (!res.includes("11")) {
            results.push(res);
        }
    }
    console.log(results.join(" "));
}
binaryStr(4);


Output

0000 0001 0010 0100 0101 1000 1001 1010


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads