<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 {
    bool vis[10];
public:
    int numDupDigitsAtMostN(int n) {
        return n - helper(n);
    }

    int helper(int n){
        int res = 0;
        string s = to_string(n);
        int len = s.size();
        for(int i=1; i<len; i++){
            res += permu(10, i) - permu(9, i-1);
        }
        return res + dfs(s, 0);
    }

    int dfs(const string& s, int cur){
        if(cur>=s.size()) return 1;

        int cur_num = s[cur]-'0';
        int cnt = 0;
        for(int i=0; i<=9; i++){
            if(i==0 && cur==0) continue;

            if(vis[i]) continue;

            if(i<cur_num) cnt += permu(10-(cur+1), s.size()-(cur+1));
            else if(i==cur_num){
                vis[i] = true;
                cnt += dfs(s, cur+1);
                vis[i] = false;
            }
        }
        return cnt;
    }

    int permu(int num, int time){
        int res = 1;
        for(int i=0; i<time; i++){
            res *= (num-i);
        }
        return res;
    }
};
7400