All problems

Problem 732

Binary Strings Without Consecutive Ones

Medium · 20 pts · Recursion, Backtracking

Problem

Generate all binary strings of length N that never contain two 1s next to each other, and print them in lexicographic order (which for equal-length binary strings is the same as increasing numeric order). Build the strings recursively from left to right: a 0 can always be appended, while a 1 can only be appended when the string is empty or ends with a 0. Trying 0 before 1 at every step produces them in the required order.

Input

A single integer N.

Output

Print all valid strings in lexicographic order, separated by spaces.

Constraints

1 <= N <= 14

Example input

3

Example output

000 001 010 100 101

Explanation

For N = 3 the strings without two neighbouring 1s are 000, 001, 010, 100 and 101. The strings 011, 110 and 111 are excluded, so the answer lists those five in order.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Permutation Sequence Next: Count Good Numbers