Count Numbers with Unique Digits (LeetCode 357): Solution and Explanation

Medium
Math
Dynamic Programming
Backtracking

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

  1. If n == 0, return 1 (only the number 0).
  2. Start total = 10 for the 1-digit numbers and choices = 9 for the leading digit of longer numbers.
  3. For each length k from 2 to n, multiply choices by 11 - k (the digits still unused for the new slot) and add it to total.
  4. Return total.

Step-by-step example

n = 2
  1. n is not 0, so start with total = 10 for the numbers 0 to 9.
  2. choices = 9: a 2-digit number has 9 options for its first digit.
  3. k = 2: the second digit must differ from the first, leaving 11 - 2 = 9 options. choices = 81.
  4. 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 total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One multiplication and one addition per length, and n is at most 8.

Space

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 = 0 the 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.