Given a string num
representing an integer, determine if it is a strobogrammatic number. A strobogrammatic number is a number that looks the same when rotated 180 degrees (upside down). The valid pairs are (0, 0), (1, 1), (6, 9), (8, 8), and (9, 6). Can you provide an algorithm and code to check if a given number is strobogrammatic?
For example:
Could you also discuss the time and space complexity of your solution, and handle potential edge cases like empty strings or null inputs? Also, how would you handle leading zeros, and how would your code behave when the number consists of only one digit like '0', '1', or '8'?
When 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:
The strobogrammatic number problem is about finding numbers that look the same when rotated 180 degrees. The brute force approach involves checking every possible number within the given length constraints to see if it is strobogrammatic.
Here's how the algorithm would work step-by-step:
def find_strobogrammatic_numbers(number_length):
result = []
def generate_numbers(current_number, length):
if length == 0:
if len(current_number) > 1 and current_number[0] == '0':
return
if len(current_number) > 0:
if is_strobogrammatic(current_number):
result.append(current_number)
return
generate_numbers(current_number + '0', length - 1)
generate_numbers(current_number + '1', length - 1)
generate_numbers(current_number + '8', length - 1)
generate_numbers(current_number + '6', length - 1)
generate_numbers(current_number + '9', length - 1)
def is_strobogrammatic(number):
left_index = 0
right_index = len(number) - 1
#Compare numbers from outside in
while left_index <= right_index:
if number[left_index] == number[right_index]:
if number[left_index] != '0' and number[left_index] != '1' and number[left_index] != '8':
return False
elif (number[left_index] == '6' and number[right_index] == '9') or \
(number[left_index] == '9' and number[right_index] == '6'):
pass
else:
return False
left_index += 1
right_index -= 1
return True
#Check to see if the number has any length at all.
if number_length > 0:
generate_numbers('', number_length)
return result
The trick to this problem is to compare the number from both ends simultaneously. We check to see if the digits match with their strobogrammatic counterpart while moving inwards, meaning comparing the outer digits first and then moving towards the center.
Here's how the algorithm would work step-by-step:
def is_strobogrammatic(number):
left_index = 0
right_index = len(number) - 1
while left_index <= right_index:
left_digit = number[left_index]
right_digit = number[right_index]
# Checking for strobogrammatic pairs
if (left_digit == '0' and right_digit == '0') or \
(left_digit == '1' and right_digit == '1') or \
(left_digit == '6' and right_digit == '9') or \
(left_digit == '8' and right_digit == '8') or \
(left_digit == '9' and right_digit == '6'):
left_index += 1
right_index -= 1
else:
# If no strobogrammatic pair is found
return False
return True
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list since there are no strobogrammatic numbers of length 0. |
Input n = 0 | Return an empty list as a strobogrammatic number cannot have length 0. |
Input n = 1 | Return the list ['0', '1', '8'] as these are the only single-digit strobogrammatic numbers. |
Input n = 2 | Return the list ['11', '69', '88', '96'] since these are the only two-digit strobogrammatic numbers (excluding '00'). |
Leading zero for n > 1 (e.g., '0x' is invalid) | When building longer numbers, ensure the first digit is not zero, unless n = 1. |
Large input n, potentially leading to memory issues | The recursive/iterative solution should build the numbers incrementally to avoid excessive memory usage and a StackOverflow error during recursion for larger n |
Odd length n with middle digit not 0, 1, or 8 | For odd lengths, the middle digit MUST be 0, 1, or 8 to form a valid strobogrammatic number. |
The final result list containing duplicate numbers | The recursive calls should avoid duplication while generating the numbers. |