<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 solve(std::string &str, int start, int len, int mask, bool isLimitPos,
              std::vector<std::vector<std::vector<int>>> &dpTable) {

        if (start >= len) return mask != 0;

        if (dpTable[start][mask][isLimitPos] != -1) return dpTable[start][mask][isLimitPos];

        int result = 0;
        int maxNumber = isLimitPos ? str[start] - '0' : 9;
        for (int number = 0; number <= maxNumber; ++number) {
            if ((mask & (1 << number)) == 0) {
                int newMask = (number == 0 && mask == 0) ? mask : mask | (1 << number);
                int newIsLimit = (number == maxNumber) && isLimitPos;
                result += solve(str, start + 1, len, newMask, newIsLimit, dpTable);
            }
        }
        return dpTable[start][mask][isLimitPos] = result;
    }
    int numDupDigitsAtMostN(int n) {
        std::string str = std::to_string(n);
        int len = static_cast<int>(str.size());
        std::vector<std::vector<std::vector<int>>> dpTable(len,
            std::vector<std::vector<int>>(1 << 10, std::vector<int>(2, -1)));
        
        return n - solve(str, 0, len, 0, true, dpTable);
    }
};
61600