Given an integer n, return all the strobogrammatic numbers of length n.
You can return the answer in any order.
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Example 1:
Input: n = 2 Output: ["11","69","88","96"]
Example 2:
Input: n = 1 Output: ["0","1","8"]
Constraints:
1 <= n <= 15When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are looking for special numbers that look the same when turned upside down. The brute force method tries every possible combination of digits to find these numbers by building them one by one.
Here's how the algorithm would work step-by-step:
def find_all_strobogrammatic_numbers_brute_force(number_length):
strobogrammatic_pairs = [('0', '0'), ('1', '1'), ('6', '9'), ('8', '8'), ('9', '6')]
def is_strobogrammatic(number_string):
left_index = 0
right_index = len(number_string) - 1
while left_index <= right_index:
if (number_string[left_index], number_string[right_index]) in strobogrammatic_pairs:
left_index += 1
right_index -= 1
else:
return False
return True
def generate_numbers(current_length, max_length):
if current_length > max_length:
return []
if current_length == max_length:
if current_length == 1:
return ['0', '1', '8']
return []
if current_length == 1:
result = ['0', '1', '8']
else:
result = [str(digit) for digit in range(10)]
strobogrammatic_numbers = []
for number in result:
if len(number) == max_length and number[0] == '0':
continue
if is_strobogrammatic(number):
strobogrammatic_numbers.append(number)
for length in range(2, max_length + 1):
new_numbers = []
for first_digit in range(10):
for last_digit in range(10):
new_number = str(first_digit) + str(last_digit)
if len(new_number) == length and new_number[0] == '0':
continue
if is_strobogrammatic(new_number):
strobogrammatic_numbers.append(new_number)
for current_number in generate_numbers(length, max_length):
if is_strobogrammatic(current_number):
strobogrammatic_numbers.append(current_number)
return strobogrammatic_numbers
all_possible_numbers = []
for length_of_number in range(1, number_length + 1):
all_numbers_of_length = [''.join(combination) for combination in product('0123456789', repeat=length_of_number)]
for number_string in all_numbers_of_length:
# Exclude numbers starting with zero if length > 1
if len(number_string) > 1 and number_string[0] == '0':
continue
# Check if the number is strobogrammatic and append if it is
if is_strobogrammatic(number_string):
all_possible_numbers.append(number_string)
from itertools import product
strobogrammatic_numbers_final = []
for length in range(1, number_length + 1):
# Iterate through all possible digit combinations.
all_combinations = [''.join(combination) for combination in product('0123456789', repeat=length)]
for number_string in all_combinations:
if len(number_string) > 1 and number_string[0] == '0':
continue
#Filter out any numbers that are not strobogrammatic
if is_strobogrammatic(number_string):
strobogrammatic_numbers_final.append(number_string)
result = []
for number_string in strobogrammatic_numbers_final:
if len(number_string) == number_length:
#Only return the numbers that match the target number_length
result.append(number_string)
return resultThe best way to find all strobogrammatic numbers of a given length is to build them systematically. We start with the shortest valid strobogrammatic numbers and then extend them outwards until we reach the desired length.
Here's how the algorithm would work step-by-step:
def find_strobogrammatic(number_length):
result = []
def extend_strobogrammatic(current_numbers, length):
if length == 0:
result.extend(current_numbers)
return
new_numbers = []
strobogrammatic_pairs = [('0', '0'), ('1', '1'), ('6', '9'), ('8', '8'), ('9', '6')]
for number in current_numbers:
for pair in strobogrammatic_pairs:
# Prevent leading zero if not single digit
if length == number_length and pair[0] == '0' and length > 1:
continue
new_numbers.append(pair[0] + number + pair[1])
extend_strobogrammatic(new_numbers, length - 2)
if number_length % 2 == 0:
extend_strobogrammatic([''], number_length)
else:
# Middle digit must be 0, 1 or 8 if odd length
extend_strobogrammatic(['0', '1', '8'], number_length - 1)
return result
def find_strobogrammatic_number_ii(number_length):
# Generate all numbers of desired length
all_numbers = find_strobogrammatic(number_length)
return all_numbers| Case | How to Handle |
|---|---|
| Input n is 0 | Return an empty list since a strobogrammatic number must have at least one digit. |
| Input n is 1 | Return the list [0, 1, 8] as these are the only single-digit strobogrammatic numbers. |
| Input n is 2 | Return the list [11, 69, 88, 96] (excluding 00). |
| Leading zero for n > 1 | Avoid constructing numbers starting with zero except for the single digit case. |
| Integer overflow with large n | String representation avoids potential integer overflow, and the problem constraints typically limit n to a reasonable size. |
| n is a large even number | The recursive approach explores all possibilities, but can be optimized with memoization to avoid redundant calculations. |
| n is a large odd number | The recursive approach builds upon the center digit (0, 1, or 8) and proceeds as with even numbers. |
| No valid strobogrammatic number exists (e.g., n < 0) | Handle invalid inputs by returning an empty list, ensuring the code gracefully handles them. |