<p>Given an integer <code>n</code>, return the count of all numbers with unique digits, <code>x</code>, where <code>0 &lt;= x &lt; 10<sup>n</sup></code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 91
<strong>Explanation:</strong> The answer should be the total numbers in the range of 0 &le; x &lt; 100, excluding 11,22,33,44,55,66,77,88,99
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= n &lt;= 8</code></li>
</ul>

class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        vector<bool> digits(10);
        int res = 0;
        unordered_map<int, unordered_map<int, int>> memo;
        for (int i = 0; i <= n; i++) {
            res += backtrack(digits, i, i, memo);
        }
        return res;
    }

    int backtrack(vector<bool>& digits, int k, int total_digits, unordered_map<int, unordered_map<int, int>>& memo) {
        if (k < 1) return 1;
        if (memo.count(k) && memo[k].count(total_digits)) return memo[k][total_digits];
        int cnt = 0;

        for (int i = 0; i < digits.size(); i++) {
            if (!digits[i] && (i != 0 || k != total_digits)) {
                digits[i] = true;
                cnt += backtrack(digits, k-1, total_digits, memo);
                digits[i] = false;
            }
        }

        memo[k][total_digits] = cnt;

        return cnt;
    }
};
8100