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

# Count Numbers with Unique Digits

> Tested Python solution for LeetCode 357 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 357, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/count-numbers-with-unique-digits/description/).

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

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

## Problem

Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10^n`.

### Examples

```
Input: n = 2
Output: 91
```

**Explanation:** The answer should be the total numbers in the range of 0 \<= x \< 100, excluding 11,22,33,44,55,66,77,88,99.

```
Input: n = 0
Output: 1
```

### Constraints

* 0 \<= n \<= 8

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def count_numbers_with_unique_digits(self, n: int) -> int:
        if n == 0:
            return 1
        total = 10
        count = 9
        available = 9
        for _ in range(2, n + 1):
            count *= available
            available -= 1
            total += count
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


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