<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 numDupDigitsAtMostN(int n) {
        if(n < 10) return 0;
        int k = 0;
        for(int i = n; i > 0; i /= 10) k++;
        int digit[k];
        for(int i = 0, j = n; i < k; i++, j /= 10) digit[k - 1 - i] = j % 10;
        
        int noDupBaseSum = 0;
        int noDupBase[k - 1];
        for(int i = 0; i < k - 1; i++)
        {
            noDupBase[i] = i == 0 ? 9 : noDupBase[i - 1] * (10 - i);
            noDupBaseSum += noDupBase[i];
        }
        
        int count[10];
        int noDupRes[k];
        bool duplicate = false;
        for(int i = 0; i < k; i++)
        {
            noDupRes[i] = i == 0 ? 9 : noDupRes[i - 1] * (10 - i);
            if(!duplicate)
            {
                int diff = 0;
                for(int j = digit[i] + 1; j < 10; j++) diff += count[j] == 0;
                noDupRes[i] -= diff;
                count[digit[i]]++;
                if(count[digit[i]] > 1) duplicate = true;
            }
        }
        return n - (noDupBaseSum + noDupRes[k - 1]);
    }
};
7500