<p>Given an array of <code>digits</code> which is sorted in <strong>non-decreasing</strong> order. You can write numbers using each <code>digits[i]</code> as many times as we want. For example, if <code>digits = [&#39;1&#39;,&#39;3&#39;,&#39;5&#39;]</code>, we may write numbers such as <code>&#39;13&#39;</code>, <code>&#39;551&#39;</code>, and <code>&#39;1351315&#39;</code>.</p>

<p>Return <em>the number of positive integers that can be generated </em>that are less than or equal to a given integer <code>n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> digits = [&quot;1&quot;,&quot;3&quot;,&quot;5&quot;,&quot;7&quot;], n = 100
<strong>Output:</strong> 20
<strong>Explanation: </strong>
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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> digits = [&quot;1&quot;,&quot;4&quot;,&quot;9&quot;], n = 1000000000
<strong>Output:</strong> 29523
<strong>Explanation: </strong>
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.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> digits = [&quot;7&quot;], n = 8
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= digits.length &lt;= 9</code></li>
	<li><code>digits[i].length == 1</code></li>
	<li><code>digits[i]</code> is a digit from&nbsp;<code>&#39;1&#39;</code>&nbsp;to <code>&#39;9&#39;</code>.</li>
	<li>All the values in&nbsp;<code>digits</code> are <strong>unique</strong>.</li>
	<li><code>digits</code> is sorted in&nbsp;<strong>non-decreasing</strong> order.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
        int_digits = [int(digit) for digit in digits]
        n_str = str(n)
        n_len = len(n_str)
        d_len = len(digits)
        
        # Count numbers with fewer digits than n
        count = sum(d_len ** i for i in range(1, n_len))
        
        # Count numbers with the same number of digits as n
        count += self.numValid(int_digits, n_str)
        
        return count

    def numValid(self, digits, n_str):
        if not n_str:
            return 1  # Base case: reached the end of recursion
        
        first_num = int(n_str[0])
        n_len = len(n_str)
        
        numDigitsLessThanN = sum(1 for digit in digits if digit < first_num)
        digitEqualToNExists = first_num in digits
        
        # Numbers that can be formed by putting a smaller digit at the first place
        number_of_combinations = numDigitsLessThanN * pow(len(digits), n_len - 1)
        
        # If the first digit of n exists in the digits, recursively check the remaining digits
        if digitEqualToNExists:
            number_of_combinations += self.numValid(digits, n_str[1:])
        
        return number_of_combinations

16300