An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300 Output: [123,234]
Example 2:
Input: low = 1000, high = 13000 Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9When 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 brute force approach to finding sequential digits involves generating every possible number within a given range and checking if it meets the required criteria. We'll start by checking single-digit numbers, then two-digit numbers, and so forth. This systematic approach ensures that we cover all potential solutions.
Here's how the algorithm would work step-by-step:
def sequential_digits_brute_force(low, high):
results = []
# Iterate through possible starting digits and lengths.
for start_digit in range(1, 10):
number = start_digit
if number >= low and number <= high:
results.append(number)
for next_digit in range(start_digit + 1, 10):
number = number * 10 + next_digit
# This avoids numbers that are longer than 9 digits
if number >= low and number <= high:
results.append(number)
# Sorting needed to return in increasing order
results.sort()
return resultsThe problem asks for numbers made of increasing sequential digits within a given range. The best approach is to directly generate these sequential digit numbers and then filter to only include the numbers within the range, avoiding unnecessary checks.
Here's how the algorithm would work step-by-step:
def sequentialDigits(low, high):
sequential_numbers_in_range = []
# Generate sequential numbers of varying lengths.
for start_digit in range(1, 10):
sequential_number = start_digit
for next_digit in range(start_digit + 1, 10):
sequential_number = sequential_number * 10 + next_digit
# Check if the generated number is within the range.
if low <= sequential_number <= high:
sequential_numbers_in_range.append(sequential_number)
#If sequential number exceeds max, stop
if sequential_number > high:
break
# Return the sorted list of sequential numbers.
return sorted(sequential_numbers_in_range)| Case | How to Handle |
|---|---|
| low is greater than high | Return an empty list since the range is invalid. |
| low and high are equal and are a sequential digit number | Add the number to the result if it satisfies the sequential digit property. |
| low is a sequential digit number but high is not | The solution needs to identify all sequential digit numbers and check whether they fall within the range. |
| high is a sequential digit number but low is not | The solution should only add numbers within the inclusive range [low, high]. |
| No sequential digit numbers exist between low and high | The solution should return an empty list if no numbers match the criteria within the range. |
| Integer overflow when constructing sequential digit numbers beyond 9 digits | Limit the construction of sequential digits to a maximum of 9 digits to avoid integer overflow. |
| low and high are very close, only needing small sequential number generation | Generate sequential numbers starting from length one and stopping once a number exceeds 'high'. |
| low and high are very far apart, generating almost all sequential numbers possible. | Generate sequential numbers starting from length one to nine, filtering within [low,high]. |