<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 dfs(self, s: str, cur: int, mask: int, is_limit: bool, is_num: bool) -> int:
        if cur == len(s):
            return 1 if is_num else 0
        elif (cur, mask, is_limit, is_num) in self.cache:
            return self.cache[(cur, mask, is_limit, is_num)]
        else:
            total = 0
            # 1. if is_num == False, we can try to skip current digit
            if is_num == False:
                total += self.dfs(s, cur+1, mask, False, is_num)
            min_digit = 1 if not is_num else 0
            max_digit = int(s[cur]) if is_limit else 9
            for digit in range(min_digit, max_digit+1):
                if (mask & (1<<digit)) != 0:
                    continue
                next_mask = (mask | (1<<digit))
                if is_limit and digit == max_digit:
                    total += self.dfs(s, cur+1, next_mask, True, True)
                else:
                    total += self.dfs(s, cur+1, next_mask, False, True)
            self.cache[(cur, mask, is_limit, is_num)] = total
            return total
    
    def numDupDigitsAtMostN(self, n: int) -> int:
        self.cache = {}
        res = self.dfs(str(n), 0, 0, True, False)
        return n-res
    
    
    
17000