Given an integer n, return all the strobogrammatic numbers that have exactly n digits, sorted in ascending order.
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Function Signature
def find_strobogrammatic(n: int) -> list[str]:
Rules
-
Return each number as a string.
-
Return the list sorted in ascending order. All strings have the same length, so string order and numeric order agree.
-
The digits 0, 1 and 8 stay the same upside down, 6 becomes 9, and 9 becomes 6. The digits 2, 3, 4, 5 and 7 have no valid upside-down form.
-
A number with more than one digit cannot start with 0. For
n = 1
, "0" counts.
Constraints
Examples
Input: n = 2
Output: ["11", "69", "88", "96"]
The two digits must turn into each other when rotated. The pair "00" is excluded because it has a leading zero.
Input: n = 1
Output: ["0", "1", "8"]
A single digit has to rotate to itself, and only 0, 1 and 8 do.
Input: n = 3
Output: ["101", "111", "181", "609", "619", "689", "808", "818", "888", "906", "916", "986"]
The outer digits are 1...1, 6...9, 8...8 or 9...6, and the middle digit is 0, 1 or 8, which gives 12 numbers (4 outer pairs times 3 middles).