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

    string s;

    int memo[15][3][3][3][3][3][3][3][3][3][3];

    int dp(int n, int digits[10], int digitsLeft, bool d, bool smaller){
        if (n > cap){
            return 0;
        }
        if (digitsLeft == 0){
            return d;
        }
        if (smaller && memo[digitsLeft][digits[0]][digits[1]][digits[2]][digits[3]][digits[4]][digits[5]][digits[6]][digits[7]][digits[8]][digits[9]] != -1){
            return memo[digitsLeft][digits[0]][digits[1]][digits[2]][digits[3]][digits[4]][digits[5]][digits[6]][digits[7]][digits[8]][digits[9]];
        }
        int ans = 0;
        int digitCap = !smaller ? s[s.size()-digitsLeft]-'0' : 9;
        

        for (int next = 0; next <= digitCap; ++next){
            if (n == 0 && next == 0){
                ans += dp(0, digits, digitsLeft-1, false, true);
            }
            else if (digits[next] == 2){
                ans += dp(n*10+next, digits, digitsLeft-1, true, smaller || next < digitCap);
            }
            else if (digits[next] == 1){
                ++digits[next];
                ans += dp(n*10+next, digits, digitsLeft-1, true, smaller || next < digitCap);
                --digits[next];
            }
            else{
                ++digits[next];
                ans += dp(n*10+next, digits, digitsLeft-1, false || d,smaller || next < digitCap);
                --digits[next];
            }
        }
        memo[digitsLeft][digits[0]][digits[1]][digits[2]][digits[3]][digits[4]][digits[5]][digits[6]][digits[7]][digits[8]][digits[9]]
 = ans;
        return ans;
    }

    int numDupDigitsAtMostN(int n) {
        memset(memo, -1, sizeof(memo));
        cap = n;
        int temp[10];
        memset(temp, 0, sizeof(temp));
        string tmp = to_string(n);
        s = tmp;
        return dp(0, temp, tmp.size(), false, false);
    }
};
11600