<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[11][2][1024][2][2] ; 
    int fun(int ind, int started, int mask, string a, string b, int lb,
            int hb) {
        
        if (ind == a.size()) {  
            return 1 ; 
        }
        if(dp[ind][started][mask][lb][hb]!=-1) return dp[ind][started][mask][lb][hb] ; 
        int low = 0, high = 9;
        if (lb) {
            low = a[ind] - '0';
        }
        if (hb) {
            high = b[ind] - '0';
        }
        int ans = 0;
        for (int j = low; j <= high; j++) {
            // this j can only be taken if its already not taken earlier
            int nlow = lb;
            int nhigh = hb;
            if (j != a[ind] - '0')
                nlow = 0;
            if (j != b[ind] - '0')
                nhigh = 0;
            if (started == 0) {
                if (j == 0) {
                    // cout<<"--"<<j<<' '<<nlow<<' '<<nhigh<<endl;
                    ans += fun(ind + 1, 0, mask, a, b, nlow, nhigh);
                } else {
                    if ((mask >> j) & 1)
                        ans +=
                            fun(ind + 1, 1, mask ^ (1 << j), a, b, nlow, nhigh);
                }
            } else {
                if ((mask >> j) & 1) {

                    ans += fun(ind + 1, 1, mask ^ (1 << j), a, b, nlow, nhigh);
                }
            }
        }
        return dp[ind][started][mask][lb][hb] = ans;
    }
    int numDupDigitsAtMostN(int n) {
        //  i can pull it off using digit dp math is easy go hard way
        string first = "1";
        string second = to_string(n);
        string temp = "";
        int diff = second.size() - first.size();
        while (diff--) {
            temp += "0";
        }

        first = temp + first;
       memset(dp,-1,sizeof dp) ; 
        return n - fun(0, 0, (1 << 10) - 1, first, second, 1, 1);
    }
};
10200