<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[20][2][2][2][1024]; 
    vector<int> num;

    int solve(int pos, int tight, int start, int rep, int mask) {
        if (pos == num.size()) {
            return rep; // Only count if there is a repeated digit
        }

        if (dp[pos][tight][start][rep][mask] != -1) {
            return dp[pos][tight][start][rep][mask];
        }

        int res = 0;
        int k = tight ? num[pos] : 9;

        for (int i = 0; i <= k; i++) {
            int ns = start || (i > 0);
            int nt = tight && (i == k);
            if (ns) {
                res += solve(pos + 1, nt, ns, rep || (mask & (1 << i)), mask | (1 << i));
            } else {
                res += solve(pos + 1, nt, 0, rep, mask);
            }
        }

        dp[pos][tight][start][rep][mask] = res;
        return res;
    }

    int numDupDigitsAtMostN(int N) {
        num.clear();
        while (N) {
            num.push_back(N % 10);
            N /= 10;
        }
        reverse(num.begin(), num.end());
        memset(dp, -1, sizeof(dp));
        return solve(0, 1, 0, 0, 0);
    }
};

8100