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

#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>

class Solution {

    int memoize (int i, int j, string& end, bool isUpperBound, vvvi& dp) {
        if (i == end.size()) return 1;
        int ub = isUpperBound ? 1:0;
        if (dp[i][j][ub] != -1) return dp[i][j][ub];
        int ans = 0;
        char to = isUpperBound ? end[i] : '9';
        for (char ch='0'; ch<=to; ch++) {
            int checkMask = (1<<(ch-'0'));
            if (j == 0 && ch=='0') ans += memoize(i+1, j, end, isUpperBound && (ch==to), dp);
            else if (checkMask & j) {
                continue;
            }
            else ans += memoize(i+1, j | checkMask, end, isUpperBound && (ch == to), dp);
        }
        dp[i][j][ub] = ans;
        return ans;
    }

public:
    int numDupDigitsAtMostN(int n) {
        string end = to_string(n);
        int noRepeats = (1<<10);
        int x = end.size();
        vvvi dp(x, vvi(noRepeats, vi(2,-1)));
        int notAns = memoize(0, 0, end, true, dp); 
        notAns--;
        return n-notAns;
    }
};
61800