Count Stepping Numbers in Range — LeetCode 2801 Python Solution

HardStringDynamic Programming
Problem
#2801
Reading time
4 min

The problem

Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high]. A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1.

Example

Input
low = "1", high = "11"
Output
10
Explanation
The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10.

Python solution

Python
class Solution:
    def countSteppingNumbers(self, low: str, high: str) -> int:
        @cache
        def dfs(pos: int, pre: int, lead: bool, limit: bool) -> int:
            if pos >= len(num):
                return int(not lead)
            up = int(num[pos]) if limit else 9
            ans = 0
            for i in range(up + 1):
                if i == 0 and lead:
                    ans += dfs(pos + 1, pre, True, limit and i == up)
                elif pre == -1 or abs(i - pre) == 1:
                    ans += dfs(pos + 1, i, False, limit and i == up)
            return ans % mod

        mod = 10**9 + 7
        num = high
        a = dfs(0, -1, True, True)
        dfs.cache_clear()
        num = str(int(low) - 1)
        b = dfs(0, -1, True, True)
        return (a - b) % mod

Complexity

MeasureComplexity
TimeO(\log M \times |\Sigma|^2)
SpaceO(\log M \times |\Sigma|), where M represents the size of the number high, and |\Sigma| represents the digit set auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 2801. Count Stepping Numbers in Range is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2801. Count Stepping Numbers in Range?
LeetCode 2801. Count Stepping Numbers in Range is rated Hard on LeetCode.
What is the time complexity of LeetCode 2801. Count Stepping Numbers in Range?
The Python solution on this page runs in O(\log M \times |\Sigma|^2).
What is the space complexity of LeetCode 2801. Count Stepping Numbers in Range?
The Python solution on this page uses O(\log M \times |\Sigma|), where M represents the size of the number high, and |\Sigma| represents the digit set auxiliary space.
What topics does LeetCode 2801. Count Stepping Numbers in Range cover?
LeetCode 2801. Count Stepping Numbers in Range is tagged String and Dynamic Programming 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.

Get Stealth Interview