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

int dp[10][1<<10][2][10];//index digit_set flag length

string s;

int solve(int ind,int mask,bool f,int len)
{
    if(ind>=len)
        return 1;
    
    auto &p=dp[ind][mask][f][len];

    if(p!=-1)
        return p;
    
    p=0;

    int maxi=9;

    if(f&&len==s.size())
        maxi=s[ind]-'0';
    
    for(int i=0;i<=maxi;++i)
    {
        if((mask&(1<<i)))
            continue;
        if(f)
        {
            bool c=(i==maxi);
            p+=solve(ind+1,mask^(1<<i),c,len);
            continue;
        }
        p+=solve(ind+1,mask^(1<<i),false,len);
    }
    return p;

}

class Solution {
public:
    int numDupDigitsAtMostN(int n) {
        s=to_string(n);
        memset(dp,-1,sizeof(dp));
        int ans=0;

        for(int len=1;len<=s.size();++len)
        {
            int maxi=9;
            if(len==s.size())
            {
                maxi=s[0]-'0';
                for(int i=1;i<=maxi;++i)
                {
                    bool f=(i==maxi);
                    ans+=solve(1,1<<i,f,len);
                }
            }
            else
            {
                for(int i=1;i<10;++i)
                {
                    ans+=solve(1,1<<i,false,len);
                }
            }
        }
        ans=n-ans;
        return ans;

    }
};
8400