MediumNo. 1291

Sequential Digits — LeetCode 1291 Python Solution

Topics
Enumeration
Reading time
2 min

The problem

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

Input
low = 100, high = 300
Output
[123,234]

Python solution

PythonSolution
class Solution:
    def sequentialDigits(self, low: int, high: int) -> List[int]:
        ans = []
        for i in range(1, 9):
            x = i
            for j in range(i + 1, 10):
                x = x * 10 + j
                if low <= x <= high:
                    ans.append(x)
        return sorted(ans)

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Frequently asked questions

How hard is LeetCode 1291. Sequential Digits?

LeetCode 1291. Sequential Digits is rated Medium on LeetCode.

What topics does LeetCode 1291. Sequential Digits cover?

LeetCode 1291. Sequential Digits is tagged Enumeration on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.