Skip to content

Binary Strings With No Adjacent Ones

Easy#backtracking#pruning~15 min

The mission

Problem

Given a length n, return every binary string of that length in which no two 1s are next to each other. Return the strings in increasing order.

Examples

Input:  n = 3
Output: ["000", "001", "010", "100", "101"]
Why:    011, 110 and 111 each put two 1s side by side
Input:  n = 1
Output: ["0", "1"]
Input:  n = 0
Output: [""]
Why:    edge case, the empty string is the one string of length zero

Hints

0 / 3

Target time
O(n · 1.62ⁿ)
Target space
O(n · 1.62ⁿ)

Stuck on the idea rather than the code? The Decision Tree covers it.

Python 3Ctrl+Enter runs
Custom test case