<p>Given an integer <code>n</code>, return <em>the number of positive integers in the range </em><code>[1, n]</code><em> that have <strong>at least one</strong> repeated digit</em>.</p>

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

<pre>
<strong>Input:</strong> n = 20
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only positive number (&lt;= 20) with at least 1 repeated digit is 11.
</pre>

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

<pre>
<strong>Input:</strong> n = 100
<strong>Output:</strong> 10
<strong>Explanation:</strong> The positive numbers (&lt;= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.
</pre>

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

<pre>
<strong>Input:</strong> n = 1000
<strong>Output:</strong> 262
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def numDupDigitsAtMostN(self, n: int) -> int:
        nstr = str(n)
        
        # number of integers <= n that have NO repeat digits
        @cache
        def dp(i, isTight, usedMask, hasLeadZero):
            if i == len(nstr):
                return 1
            
            end = int(nstr[i]) if isTight else 9
            ret = 0
            for digit in range(end + 1):
                digitUsed = (usedMask >> digit) & 1
                if digitUsed and not (digit == 0 and hasLeadZero):
                    continue
                
                if not (hasLeadZero and digit == 0):
                    newUsedMask = usedMask | (1 << digit)
                else:
                    # if adding leading zero, don't add 0 to used set
                    newUsedMask = usedMask
                ret += dp(
                    i + 1,
                    isTight and digit == end,
                    newUsedMask,
                    hasLeadZero and digit == 0
                )
            return ret
        
        # +1 for 0
        return n + 1 - dp(0, True, 0, True)
21800