Generate Binary Strings Without Adjacent Zeros - Problem
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.
In other words, no two consecutive zeros ("00") are allowed in any valid binary string.
Input & Output
Example 1 — Basic Case
$
Input:
n = 3
›
Output:
["010","011","101","110","111"]
💡 Note:
All valid 3-length binary strings without consecutive zeros. Note that "000", "001", and "100" are invalid because they contain "00".
Example 2 — Minimum Size
$
Input:
n = 1
›
Output:
["0","1"]
💡 Note:
For length 1, both "0" and "1" are valid since there are no substrings of length 2 to check.
Example 3 — Small Case
$
Input:
n = 2
›
Output:
["01","10","11"]
💡 Note:
For length 2, "00" is invalid (contains consecutive zeros), but "01", "10", and "11" are all valid.
Constraints
- 1 ≤ n ≤ 20
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given n=3, need all valid binary strings of length 3
2
Constraint
No substring '00' allowed - must have at least one '1' in every 2-char window
3
Output
All valid strings: '010', '011', '101', '110', '111'
Key Takeaway
🎯 Key Insight: After placing a '0', the next position must be '1' to avoid consecutive zeros
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code