> ## 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.

# Strobogrammatic Number III Python Solution

> Tested Python solution for LeetCode 248 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 248, [Hard](/catalog/hard). Topics: [Recursion](/catalog/topics/recursion), [Array](/catalog/topics/array), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/strobogrammatic-number-iii/description/).

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

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

## Problem

Given two strings low and high that represent two integers `low` and `high` where `low <= high`, return *the number of **strobogrammatic numbers** in the range* `[low, high]`.

A **strobogrammatic number** is a number that looks the same when rotated `180` degrees (looked at upside down).

### Examples

```
Input: low = "50", high = "100"
Output: 3
```

```
Input: low = "0", high = "0"
Output: 1
```

### Constraints

* `1 <= low.length, high.length <= 15`
* `low` and `high` consist of only digits.
* `low <= high`
* `low` and `high` do not contain any leading zeros except for zero itself.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(2^n * n)
    # Space: O(2^n)
    def strobogrammatic_in_range(self, low: str, high: str) -> int:
        def build(length: int, outermost: bool) -> list[str]:
            if length == 0:
                return [""]
            if length == 1:
                return ["0", "1", "8"]
            results: list[str] = []
            for middle in build(length - 2, False):
                for left, right in ("11", "88", "69", "96"):
                    results.append(left + middle + right)
                if not outermost:
                    results.append("0" + middle + "0")
            return results

        count = 0
        for length in range(len(low), len(high) + 1):
            for candidate in build(length, True):
                if int(low) <= int(candidate) <= int(high):
                    count += 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(2^n \* n) | O(2^n) |

## Tags


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