<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 {
public:
    int dp[11][1025][2];
    int help(int i, int mask, int cc, string s) {
        if (i == s.size())
            return 0;
        if(dp[i][mask][cc]!=-1) return dp[i][mask][cc];
        int ans = 0;
        int x = int(s[i] - '0');
        if(cc) x=10;
        for (int digit = 0; digit < x; digit++) {
            if ((mask >> digit) & 1) {
                ans += pow(10, s.size() - i - 1);
            } else {
                if (digit == 0) {
                    if (__builtin_popcount(mask))
                        ans += help(i + 1, mask | (1 << digit), 1, s);
                    else
                        ans += help(i + 1, mask, 1, s);
                } else
                    ans += help(i + 1, mask | (1 << digit), 1, s);
            }
        }
        if (cc == 0) {
            if ((mask >> x) & 1){
                ans++;
                int j=i+1;
                while(j<s.size()){
                    ans+= int(s[j]-'0')*pow(10, s.size()-j-1);
                    j++;
                }
            }
                
            else
                ans += help(i + 1, mask | (1 << x), 0, s);
        }
        return dp[i][mask][cc]=ans;
    }
    int numDupDigitsAtMostN(int n) {
        memset(dp, -1, sizeof(dp));
        string s = to_string(n);
        return help(0, 0, 0, s);
    }
};
10000