Skip to content

Latest commit

 

History

History
89 lines (70 loc) · 2.11 KB

_3211. Generate Binary Strings Without Adjacent Zeros.md

File metadata and controls

89 lines (70 loc) · 2.11 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Completed during Weekly Contest 405 (q2)

Back to top


First completed : July 07, 2024

Last updated : July 09, 2024


Related Topics : String, Bit Manipulation, Recursion

Acceptance Rate : 86.6 %


Solutions

JavaScript

/**
 * @param {number} n
 * @return {string[]}
 */
var validStrings = function(n) {
    const outputs = [];

    function propogate(curr, r) {
        if (r === 0) {
            outputs.push(curr.join(''));
            return;
        }

        if (r == 1) {
            outputs.push(curr.join('') + '0');
            outputs.push(curr.join('') + '1');
            return;
        }

        curr.push('1');
        propogate(curr, r - 1);
        curr.pop();
        curr.push('01');
        propogate(curr, r - 2);
        curr.pop();
    }

    propogate([], n);
    return outputs;
};

Python

class Solution:
    def validStrings(self, n: int) -> List[str]:
        output = []

        def dfs(curr: List[str], 
                output: List[str], 
                remaining: int, 
                prevIsOne: bool) -> None :
            if not remaining :
                output.append(''.join(curr))
                return
            
            curr.append('1')
            remaining -= 1
            dfs(curr, output, remaining, True)
            curr.pop()

            if prevIsOne :
                curr.append('0')
                dfs(curr, output, remaining, False)
                curr.pop()
        dfs([], output, n, True)
        return output