Skip to content
BytePatterns

Upside-Down Numbers of a Given Length

MediumRecursion#recursion#build-from-inside-out~25m

Problem

A clock maker wants every number that still reads correctly when the display is turned upside down. Rotated by 180 degrees, 0, 1 and 8 stay the same, 6 becomes 9 and 9 becomes 6, and every other digit becomes unreadable. Given n between 1 and 14, return all n-digit numbers that look the same after the rotation, as strings in ascending order. A number longer than one digit may not start with 0.

Examples

Input:  n = 2
Output: ["11", "69", "88", "96"]
Why:    "00" is excluded because of the leading zero
Input:  n = 1
Output: ["0", "1", "8"]
Why:    edge case, a lone middle digit must map to itself, so 6 and 9 cannot be used
Input:  n = 3
Output: ["101", "111", "181", "609", "619", "689", "808", "818", "888", "906", "916", "986"]

Hints

0 / 3

Stuck on the idea rather than the code? Return Up or Pass Down covers it.