> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Stepping Numbers Python Solution with Tests

> Tested Python solution for LeetCode 1215 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 1215, [Medium](/catalog/medium). Topics: [Breadth-First Search](/catalog/topics/breadth-first-search), [Math](/catalog/topics/math), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/stepping-numbers/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1215   # by problem number
lcpy gen -s stepping_numbers   # by problem name
```

## Problem

A **stepping number** is an integer such that all of its adjacent digits have an absolute difference of exactly `1`.

* For example, `321` is a **stepping number** while `421` is not.

Given two integers `low` and `high`, return *a sorted list of all the **stepping numbers** in the inclusive range* `[low, high]`.

### Examples

```
Input: low = 0, high = 21
Output: [0,1,2,3,4,5,6,7,8,9,10,12,21]
```

```
Input: low = 10, high = 15
Output: [10,12]
```

### Constraints

* `0 <= low <= high <= 2 * 10^9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/stepping_numbers/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/stepping_numbers/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(2^log(high)) - at most ~10k stepping numbers below 2 * 10^9
    # Space: O(2^log(high)) for the queue
    def count_stepping_numbers(self, low: int, high: int) -> list[int]:
        ans: list[int] = []
        if low == 0:
            ans.append(0)
        q: deque[int] = deque(range(1, 10))
        while q:
            v = q.popleft()
            if v > high:
                break
            if v >= low:
                ans.append(v)
            last = v % 10
            if last > 0:
                q.append(v * 10 + last - 1)
            if last < 9:
                q.append(v * 10 + last + 1)
        return ans
```

## Complexity

| Time | Space |
| - | - |
| O(2^log(high)) - at most \~10k stepping numbers below 2 \* 10^9 | O(2^log(high)) for the queue |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.