A sequential digit number has digits that increase by exactly 1 from left to right (like 123 or 4567). The straightforward approach is to check every number in the range [low, high] and verify if it has sequential digits by comparing adjacent digit characters. While simple to implement, this becomes impractical for large ranges.
low to high.Instead of checking every number, we can generate only the valid sequential digit numbers directly. For a number with d digits starting with digit s, we can build it by appending consecutive digits. For example, starting with 3 and building a 4-digit number gives us 3456. We iterate over all valid digit lengths and starting digits, constructing each candidate and checking if it falls within [low, high].
low to that of high.d:s from 1 to 9:s + d > 10, break (not enough consecutive digits available).s and appending s+1, s+2, etc.[low, high], add it to the result.class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
low_digit, high_digit = len(str(low)), len(str(high))
for digits in range(low_digit, high_digit + 1):
for start in range(1, 10):
if start + digits > 10:
break
num = start
prev = start
for i in range(digits - 1):
num = num * 10
prev += 1
num += prev
if low <= num <= high:
res.append(num)
return resSince, we have at most valid numbers as per the given constraints.
We can think of generating sequential digit numbers as a BFS traversal. Start with single digits 1 through 9 in a queue. For each number, we can extend it by appending the next consecutive digit (if the last digit is less than 9). Processing the queue level by level naturally generates numbers in increasing order of length, and within each length, in increasing order of value.
n.n > high, skip it.n is within [low, high], add it to the result.n. If it's less than 9, enqueue n * 10 + (lastDigit + 1).class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
queue = deque(range(1, 10))
while queue:
n = queue.popleft()
if n > high:
continue
if low <= n <= high:
res.append(n)
ones = n % 10
if ones < 9:
queue.append(n * 10 + (ones + 1))
return resSince, we have at most valid numbers as per the given constraints.
DFS provides another way to enumerate sequential digit numbers. Starting from each single digit (1 to 9), we recursively extend the number by appending the next digit. The recursion naturally explores all valid sequential numbers. Since we start from different initial digits and explore in order, the results may not be sorted, so we sort at the end.
high, return.[low, high], add it to the result.num * 10 + (lastDigit + 1).class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
def dfs(num):
if num > high:
return
if low <= num <= high:
res.append(num)
last_digit = num % 10
if last_digit < 9:
dfs(num * 10 + (last_digit + 1))
for i in range(1, 10):
dfs(i)
return sorted(res)Since, we have at most valid numbers as per the given constraints.
All sequential digit numbers are substrings of "123456789". A 2-digit sequential number is a substring of length 2, a 3-digit one is length 3, and so on. By sliding a window of each valid length across this master string and converting substrings to integers, we generate all possible sequential digit numbers directly.
nums = "123456789".d from 2 to 9:nums:d starting at index i.high, break the inner loop.[low, high], add it to the result.Since, we have at most valid numbers as per the given constraints.
A common mistake is using a brute force approach that iterates through every number in the range [low, high]. Since there are only 36 possible sequential digit numbers (from "12" to "123456789"), iterating through potentially billions of numbers is extremely inefficient. Recognize that you should generate only the valid candidates.
When building sequential digit numbers of length d, the starting digit cannot exceed 10 - d. For example, a 4-digit sequential number cannot start with 7, 8, or 9 because there are not enough consecutive digits remaining. Failing to enforce this constraint leads to invalid numbers like "7890" which wraps around.
Some approaches like DFS generate numbers in an order that is not sorted (e.g., 12, 123, 1234, ..., 23, 234, ...). The problem expects results in ascending order. Remember to sort the final result if your generation method does not naturally produce sorted output.