Skip to content
BytePatterns

Binary Strings With No Adjacent Ones

EasyBacktracking#backtracking#pruning~15m

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

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