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
Generate Valid Binary Strings (n=3): No Adjacent ZerosAll Possible 3-bit Strings:000❌ 00001❌ 00010✓ Valid011✓ Valid100❌ 00101✓ Valid110✓ Valid111✓ ValidValid Strings (No "00" substring)["010", "011", "101", "110", "111"]Constraint: Every 2-character window must contain at least one "1"
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
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
12.5K Views
Medium Frequency
~15 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen