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:
To solve this problem using a brute force approach, we want to look at every single number we can possibly form where the digits go up in order. We then check each of those numbers to see if it falls within the range we were given, and keep the ones that fit.
Here's how the algorithm would work step-by-step:
def sequentialDigits(low: int, high: int) -> list[int]:
# Generate all possible sequential digit numbers across all valid lengths to check against range
all_sequential = []
sample_string = "123456789"
for length in range(1, 10):
for start_index in range(10 - length):
substring = sample_string[start_index:start_index + length]
all_sequential.append(int(substring))
# Filter the generated numbers to keep only those that fall strictly within the target limits
valid_numbers = []
for current_number in all_sequential:
if low <= current_number <= high:
valid_numbers.append(current_number)
# Sort the filtered collection in ascending order to satisfy the final ordering requirements
valid_numbers.sort()
return valid_numbersInstead of checking every single number to see if it fits the rules, the optimal strategy generates only the numbers that have sequentially rising digits. By building these special numbers systematically from scratch, we can quickly find all the ones that fall within our target range.
Here's how the algorithm would work step-by-step:
class Solution:
def sequential_digits(self, low_limit: int, high_limit: int) -> list[int]:
valid_sequential_numbers = []
# Generate all numbers with sequential digits systematically rather than checking every integer.
for start_digit in range(1, 10):
current_number = start_digit
for next_digit in range(start_digit + 1, 10):
current_number = current_number * 10 + next_digit
if current_number > high_limit:
break
if current_number >= low_limit:
valid_sequential_numbers.append(current_number)
# Sorting ensures the generated numbers are returned in strictly ascending order as required.
return sorted(valid_sequential_numbers)| Case | How to Handle |
|---|---|
| Low and high are equal and form a valid sequential digit number | The algorithm will generate this number and include it in the result list if it falls within the range. |
| No sequential digits exist within the given low and high range | The result list remains empty and is returned correctly without any errors. |
| Inputs at the extreme constraints like low equals 10 and high equals 10 to the power of 9 | Using 32-bit or 64-bit integers prevents overflow since the maximum valid sequential number is 123456789 which fits easily in standard integer types. |
| Range is very narrow and excludes all possible sequential numbers | The filtering step naturally omits any generated numbers that fall outside the boundaries. |
| Low is greater than high | The generator loop will not execute and an empty list will be returned immediately. |
| Single digit numbers like low equals 1 and high equals 9 | Single digits do not have a previous digit to compare against so sequential digit generation typically starts at length two unless explicitly handled. |
| Generated sequential number exceeds the high limit | A break condition stops the generation process as soon as numbers exceed the upper limit since they are produced in strictly increasing order. |