Watch 10 video solutions for Generate Binary Strings Without Adjacent Zeros, a medium level problem involving String, Backtracking, Bit Manipulation. This walkthrough by Ayush Rao has 3,231 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a positive integer n.
A binary string x is valid if all substrings of x of length 2 contain at least one "1".
Return all valid strings with length n, in any order.
Example 1:
Input: n = 3
Output: ["010","011","101","110","111"]
Explanation:
The valid strings of length 3 are: "010", "011", "101", "110", and "111".
Example 2:
Input: n = 1
Output: ["0","1"]
Explanation:
The valid strings of length 1 are: "0" and "1".
Constraints:
1 <= n <= 18Problem Overview: Generate all binary strings of length n such that no two 0s appear next to each other. Every valid string must avoid the pattern "00". The output should contain all possible strings that satisfy this constraint.
Approach 1: Backtracking (O(2^n) time, O(n) space)
Backtracking builds the string one character at a time while enforcing the constraint early. At each position you try to append '1' or '0'. The only restriction: you cannot place '0' if the previous character was also '0'. This pruning prevents invalid branches like "00" from ever forming. The recursion continues until the string length reaches n, at which point the candidate is added to the result list. The algorithm explores at most two branches per step, but many are pruned, so the number of generated strings follows a Fibonacci-like pattern rather than the full 2^n. The recursion stack stores at most n characters, so auxiliary space is O(n). This approach is a classic application of backtracking combined with simple string construction.
Approach 2: Dynamic Programming Construction (O(n * F(n)) time, O(F(n)) space)
Dynamic programming can generate valid strings iteratively using the observation that a valid string depends on its last character. Maintain two groups for each length: strings ending with '1' and strings ending with '0'. If a string ends with '1', you may append either '0' or '1'. If it ends with '0', you may only append '1'. Starting from length 1 (["0", "1"]), repeatedly build the next length by applying these transitions. The number of valid strings follows Fibonacci growth, so generation time is proportional to the total output size F(n). This approach removes recursion and makes the state transition explicit, which can be easier to reason about in iterative implementations or when applying bit manipulation style construction.
Recommended for interviews: The backtracking solution is what most interviewers expect. It demonstrates that you can generate combinations while pruning invalid states early. A brute-force idea that generates all 2^n strings and filters "00" shows baseline understanding, but backtracking proves you can enforce constraints during generation and avoid unnecessary work.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking | O(2^n) worst case (≈ Fibonacci outputs) | O(n) | Best for interviews and direct generation of valid strings with pruning |
| Dynamic Programming Construction | O(n * F(n)) | O(F(n)) | When you want an iterative solution without recursion |