1. Problem Summary
You are given two integers low and high.
Your task is to return all numbers within this range (inclusive) that have sequential digits, meaning:
- Digits must be strictly increasing
- Each digit must be exactly one more than the previous digit
- Digits must appear in order without gaps or breaks
Examples of sequential digits:
12, 23, 34, 45, 56, 67, 78, 89 123, 234, 345, 456, 567, 678, 789 1234, 2345, ...
Example interpretation:
If low = 100 and high = 300, valid results include:
123, 234
2. Key Insights
Sequential digit numbers are limited
There are very few valid sequential numbers because:
- Maximum sequence is 123456789
- Digits can only start from 1 to 9
- Length ranges from 2 digits up to 9 digits
Sequential numbers can be generated without checking every number
Instead of scanning all numbers from low to high, we generate all possible sequential-digit numbers and filter them.
Patterns based on length
A sequential number of length L always comes from a substring of:
"123456789"
For example:
- length 2 → “12”, “23”, …, “89”
- length 3 → “123”, “234”, …, “789”
- length 4 → “1234”, …, “6789”
Sorted output is natural
Since sequences are generated in increasing order, results will already be sorted.
3. Approach
Generate and filter
Steps:
- Convert
lowandhighto digit lengths. - For each possible length L (from digits in low to digits in high):
- Slide over
"123456789"extracting substrings of length L. - Convert each substring to integer.
- If it lies within
[low, high], add to result list.
- Slide over
- Return the final list.
This avoids brute-force iteration and guarantees correctness.
4. Time and Space Complexity
Time Complexity: O(1)
Reason:
- At most 36 sequential numbers exist for all ranges
- Work does not scale with input magnitude
Space Complexity: O(1)
Aside from output storage, no dynamic structures needed.
5. Java Solution
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> sequentialDigits(int low, int high) {
List<Integer> result = new ArrayList<>();
String digits = "123456789";
int lowLen = String.valueOf(low).length();
int highLen = String.valueOf(high).length();
for (int length = lowLen; length <= highLen; length++) {
for (int start = 0; start + length <= 9; start++) {
int num = Integer.parseInt(digits.substring(start, start + length));
if (num >= low && num <= high) {
result.add(num);
}
}
}
return result;
}
}
6. Dry Run Examples
Example 1
Input:
low = 100 high = 300
Lengths to consider:
- low = 100 → 3 digits
- high = 300 → 3 digits
Generate sequential numbers of length 3:
Substring selections:
- “123” → 123 (valid)
- “234” → 234 (valid)
- “345” → 345 (too large → ignore)
Final output:
[123, 234]
Example 2
Input:
low = 1000 high = 13000
Lengths: 4 and 5
Length 4 sequences:
1234 → valid 2345 → valid 3456 → valid 4567 → valid 5678 → valid 6789 → valid
Length 5 sequences:
12345 → valid 23456 → valid 34567 → 34567 > high → ignore remaining
Output:
[1234, 2345, 3456, 4567, 5678, 6789, 12345, 23456]
Example 3
Input:
low = 10 high = 20
Length 2 sequences:
12 → valid
Output:
[12]
7. Why This Solution Works
- Generates only valid structured numbers
- Avoids iterating through entire numeric range
- Automatically sorted output
- Efficient and clean
- Covers all digit lengths in range
- Uses string slicing for simplicity
8. Common Mistakes
- Brute-forcing every number between low and high
(slow for large ranges) - Forgetting that sequential digits cannot wrap (e.g., 789 → cannot become 890)
- Including numbers outside range
- Sorting unnecessarily (already sorted)
- Miscalculating length boundaries
