<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][2][1025][2][2]; 

    int rec(int ii, int lt, int ht, int mask, int done, int flag, string& num1, string& num2) {
        if(ii == num1.size()){
            return done; 
        }

        if(dp[ii][lt][ht][mask][done][flag] != -1) {
            return dp[ii][lt][ht][mask][done][flag]; 
        }

        int ans = 0; 

        int lo = 0; 
        if(lt) lo = num1[ii]-'0'; 
        int hi = 9; 
        if(ht) hi = num2[ii]-'0'; 

        for(int i = lo; i <= hi; i++) {
            int nlt = lt; 
            if(i > lo) nlt = 0; 
            int nht = ht; 
            if(i < hi) nht = 0; 
            int dones = done;

                // cout << mask << " " << ii << " " << flag << " " << i << endl; 
            if((mask&(1<<i)) && flag == 1) {
                dones = 1; 
            }
            int new_mask = mask; 
            int new_flag = flag; 
            if(i > 0) {
                new_flag = 1; 
            }

            if(new_flag == 1) {
                new_mask = mask|(1<<i);
            }


            
            ans += rec(ii+1, nlt, nht, new_mask, dones, new_flag, num1, num2); 
        }

        return dp[ii][lt][ht][mask][done][flag] = ans; 
    }

    int numDupDigitsAtMostN(int n) {
        memset(dp, -1, sizeof(dp)); 
        string num1 = "1", num2 = ""; 

        while(n > 0) {
            num2 += to_string((n%10)); 
            n /= 10; 
        }
        reverse(num2.begin(), num2.end()); 
        string temp = ""; 
        for(int i = 0; i < num2.size()-num1.size(); i++) {
            temp += "0"; 
        }
        num1 = temp + num1; 
        // cout << num1 << " " << num2 << endl; 
        return rec(0, 1, 1, 0, 0, 0, num1, num2);
        return 0;  
    }
};
8700