<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][2][1024][2][2];
    int count1(string x,int idx,int tight,int masks,int repeat,int zero)
    {
        if(idx==x.length())
        {
            if(repeat==1)
            {
                return 1;
            }
            else
            return 0;
        }
        if(dp[idx][tight][masks][repeat][zero]!=-1)
        {
            return dp[idx][tight][masks][repeat][zero];
        }
        int ans=0;
        int k=tight==1?x[idx]-'0':9;
        for(int i=0;i<=k;i++)
        {
            int new_tight=tight && (i==k)?1:0;
            int new_repeat=(!zero&&(repeat||((masks>>i)&1)>0))?1:0;
            if(zero==1 && i==0)
            ans+=(count1(x,idx+1,new_tight, masks,new_repeat,zero && (i==0)));
            else
            ans+=(count1(x,idx+1,new_tight,masks|(1<<i),new_repeat,zero && (i==0)));
        }
        return dp[idx][tight][masks][repeat][zero]=ans;
    }
    int numDupDigitsAtMostN(int n) {
        string x=to_string(n);
        memset(dp,-1,sizeof(dp));
        return count1(x,0,1,0,0,1);
    }
};
10300