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

# 数位DP+bitmask
# 将题目转为求[1, n]范围内没有重复位正整数的个数；这题大致做法和#233类似；不同点是因为这次多一个参数isNum，用来区分高位0表示位置为空而不是占了0的位置。
from functools import cache
class Solution:
    def numDupDigitsAtMostN(self, n: int) -> int:
        s = str(n)
        @cache
        def func(i, mask, isLimit, isNum):
            if i == min(len(s), 10):
                return 1
            ret = 0
            top = int(s[i]) if isLimit else 9
            for d in range(top + 1):
                bit = 1 << d
                if mask&bit:
                    continue
                isN = isNum or d>0
                new_mask = mask|bit if isN else mask
                ret += func(i+1, new_mask, isLimit and d==top, isN)
            return ret
        # 注意，这里non_repeat会多算一个0的情况，0不在取值范围中，所以要去掉。
        non_repeat = func(0, 0, True, False) - 1
        return n - non_repeat
21800