A strobogrammatic number looks the same when rotated 180 degrees. Only certain digit pairs maintain their appearance after rotation: (0,0), (1,1), (6,9), (8,8), and (9,6). We can build these numbers recursively from the inside out. Start with the base cases: empty string for even length or single symmetric digits (0, 1, 8) for odd length. Then wrap each smaller solution with valid digit pairs. The key insight is that leading zeros are invalid, so we skip adding '0' at the outermost layer.
n:n == 0, return an empty string (the center for even-length numbers).n == 1, return the three single-digit strobogrammatic numbers: "0", "1", "8".n - 2.'0' when building the outermost layer (when n equals the final target length) to avoid leading zeros.class Solution:
def findStrobogrammatic(self, n: int) -> List[str]:
reversible_pairs = [
['0', '0'], ['1', '1'],
['6', '9'], ['8', '8'], ['9', '6']
]
def generate_strobo_numbers(n, final_length):
if n == 0:
# 0-digit strobogrammatic number is an empty string.
return [""]
if n == 1:
# 1-digit strobogrammatic numbers.
return ["0", "1", "8"]
prev_strobo_nums = generate_strobo_numbers(n - 2, final_length)
curr_strobo_nums = []
for prev_strobo_num in prev_strobo_nums:
for pair in reversible_pairs:
if pair[0] != '0' or n != final_length:
curr_strobo_nums.append(pair[0] + prev_strobo_num + pair[1])
return curr_strobo_nums
return generate_strobo_numbers(n, n)Time complexity:
Space complexity:
Where is the length of strobogrammatic numbers we need to find.
Instead of recursion, we can build strobogrammatic numbers iteratively using a BFS-like approach. Start from the center and expand outward level by level. For odd-length numbers, begin with single digits (0, 1, 8). For even-length numbers, begin with an empty string. Each iteration adds two digits to the current strings, growing them symmetrically until we reach the target length.
n is odd, start with ["0", "1", "8"]; if even, start with [""].n % 2.n:2 (adding one digit to each end).'0' as the leading digit when at the final length.class Solution:
def findStrobogrammatic(self, n: int) -> List[str]:
reversible_pairs = [
['0', '0'], ['1', '1'],
['6', '9'], ['8', '8'], ['9', '6']
]
# When n is even (n % 2 == 0), we start with strings of length 0 and
# when n is odd (n % 2 == 1), we start with strings of length 1.
curr_strings_length = n % 2
q = ["0", "1", "8"] if curr_strings_length == 1 else [""]
while curr_strings_length < n:
curr_strings_length += 2
next_level = []
for number in q:
for pair in reversible_pairs:
if curr_strings_length != n or pair[0] != '0':
next_level.append(pair[0] + number + pair[1])
q = next_level
return qTime complexity:
Space complexity:
Where is the length of strobogrammatic numbers we need to find.
Numbers cannot have leading zeros (except for "0" itself when n=1). When building the outermost layer of digits, you must skip the (0, 0) pair to avoid generating invalid numbers like "010" or "00100".
Unlike 0, 1, and 8 which map to themselves when rotated 180 degrees, 6 becomes 9 and 9 becomes 6. The reversible pairs must include both (6, 9) and (9, 6) to correctly generate all strobogrammatic numbers.
For even-length numbers, the recursive base case is an empty string (length 0). For odd-length numbers, the center digit must be strobogrammatic by itself: only 0, 1, or 8. Mixing up these base cases or forgetting the odd-length center constraint will produce incorrect results.