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

# Numbers At Most N Given Digit Set

> Tested Python solution for LeetCode 902 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 902, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [String](/catalog/topics/string), [Binary Search](/catalog/topics/binary-search), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/description/).

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

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

## Problem

Given an array of `digits` which is sorted in **non-decreasing** order. You can write numbers using each `digits[i]` as many times as we want. For example, if `digits = ['1','3','5']`, we may write numbers such as `'13'`, `'551'`, and `'1351315'`.

Return the number of positive integers that can be generated that are less than or equal to a given integer `n`.

### Examples

```
Input: digits = ["1","3","5","7"], n = 100
Output: 20
```

**Explanation:** The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.

```
Input: digits = ["1","4","9"], n = 1000000000
Output: 29523
```

**Explanation:** We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers, 81 four digit numbers, 243 five digit numbers, 729 six digit numbers, 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers. In total, this is 29523 integers that can be written using the digits array.

```
Input: digits = ["7"], n = 8
Output: 1
```

### Constraints

* 1 \<= digits.length \<= 9
* digits\[i].length == 1
* digits\[i] is a digit from '1' to '9'.
* All the values in digits are unique.
* digits is sorted in non-decreasing order.
* 1 \<= n \<= 10^9

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n * len(digits))
    # Space: O(log n)
    def at_most_n_given_digit_set(self, digits: list[str], n: int) -> int:
        ds = sorted(int(d) for d in digits)
        s = str(n)
        k = len(s)
        allowed = set(ds)
        total = sum(len(ds) ** length for length in range(1, k))
        for i, ch in enumerate(s):
            total += sum(d < int(ch) for d in ds) * len(ds) ** (k - 1 - i)
            if int(ch) not in allowed:
                break
        else:
            total += 1
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(log n \* len(digits)) | O(log n) |

## Tags


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