Count Numbers with Unique Digits (LeetCode 357): Solution and Explanation
Given n, count the integers x with 0 <= x < 10^n whose digits are all different. With n = 2 the answer is 91, because 11, 22, ..., 99 are the only repeats below 100.
Intuition
Listing every number up to 10^8 and checking its digits is far too slow, but the structure is simple: a number with distinct digits is a sequence of digit slots where each slot avoids the digits already used.
Split the numbers by length. A number with exactly k digits cannot start with 0, so the first slot has 9 options. The second slot can use 0 but not the first digit, so it also has 9. The third has 8, the fourth 7, and so on.
That makes the count for length k equal to 9 x 9 x 8 x ... x (11 - k). Each length's count is the previous one times one more factor, so one running product covers every length.
The 1-digit case is special because 0 itself is a valid number, which gives 10 rather than 9. Add the lengths up and you have the answer.
Approach: Counting by length with a running product
- If
n == 0, return 1 (only the number 0). - Start
total = 10for the 1-digit numbers andchoices = 9for the leading digit of longer numbers. - For each length
kfrom 2 ton, multiplychoicesby11 - k(the digits still unused for the new slot) and add it tototal. - Return
total.
Step-by-step example
n = 2
nis not 0, so start withtotal = 10for the numbers 0 to 9.choices = 9: a 2-digit number has 9 options for its first digit.k = 2: the second digit must differ from the first, leaving11 - 2 = 9options.choices = 81.total = 10 + 81 = 91, which is the answer.
Code: Python, Java, C++
1class Solution:
2 def countNumbersWithUniqueDigits(self, n: int) -> int:
3 if n == 0:
4 return 1
5 total = 10
6 choices = 9
7 for k in range(2, n + 1):
8 choices *= 11 - k
9 total += choices
10 return total1class Solution {
2 public int countNumbersWithUniqueDigits(int n) {
3 if (n == 0) {
4 return 1;
5 }
6 int total = 10;
7 int choices = 9;
8 for (int k = 2; k <= n; k++) {
9 choices *= 11 - k;
10 total += choices;
11 }
12 return total;
13 }
14}1class Solution {
2public:
3 int countNumbersWithUniqueDigits(int n) {
4 if (n == 0) {
5 return 1;
6 }
7 int total = 10;
8 int choices = 9;
9 for (int k = 2; k <= n; k++) {
10 choices *= 11 - k;
11 total += choices;
12 }
13 return total;
14 }
15};Time and space complexity
O(n)
One multiplication and one addition per length, and n is at most 8.
O(1)
Only the running product and the total are kept.
Other approaches
Backtracking over digits (time O(answer), space O(n))
Build numbers digit by digit with a used mask and count every valid prefix. It visits each counted number once, so it is millions of steps for n = 8 but still shows the same structure.
Brute force check (time O(10^n x n), space O(1))
Loop over every x below 10^n and test for repeated digits with a set. Fine for small n, hopeless for n = 8.
Common mistakes
- Giving the leading digit 10 options. A multi-digit number cannot start with 0, so it has 9.
- Forgetting that 0 itself counts. The 1-digit group has 10 members, and for
n = 0the answer is 1, not 0. - Letting the loop run past 10 digits. With more than 10 digits a repeat is forced; the constraint n <= 8 avoids it, but a general version should stop at 10.
- Counting only numbers with exactly n digits instead of every length up to n.
Frequently asked questions
Why is the second slot also 9 options?
The second digit may be 0, which the first could not, but it may not repeat the first digit. Ten digits minus one used digit leaves 9.
Is this really dynamic programming?
It is a one-dimensional recurrence: count(k) = count(k - 1) x (11 - k). Keeping only the last value turns the DP table into a single variable.
What happens for n above 10?
No number with 11 or more digits can have all distinct digits, so the total stops growing at n = 10, where it is 8877691.