AlgoMaster Logo

Count Numbers with Unique Digits

Last Updated: March 28, 2026

medium
5 min read

Understanding the Problem

We need to count how many integers in the range [0, 10^n) have all distinct digits. For example, 123 has unique digits but 112 does not because the digit 1 repeats.

The key observation is that this is fundamentally a counting problem, not a search problem. We do not need to generate every number and check it. Instead, we can count how many k-digit numbers have all unique digits for each valid k, and sum those counts up. This shifts the problem from brute force enumeration to combinatorics.

Notice that the constraint n <= 8 is extremely small. This means even an exponential approach would work, but it also hints that there is likely a clean mathematical formula since we only have 10 possible digits (0 through 9) and at most 8 digit positions to fill.

Key Constraints:

  • 0 <= n <= 8 → With n at most 8, the range is at most [0, 10^8). That is 100 million numbers. Iterating through all of them and checking each one is borderline but likely too slow for a clean solution. However, the small value of n itself (at most 8) means a mathematical or DP approach that runs in O(n) is ideal.
  • n can be 0 → Edge case where the range is [0, 1), containing only the number 0.

Approach 1: Brute Force (Check Every Number)

Intuition

The most straightforward approach: iterate through every number from 0 to 10^n - 1 and check whether it has all unique digits. To check uniqueness, we can extract each digit and use a set to detect duplicates.

This is simple to implement and easy to reason about. For small n (like 0, 1, or 2), it works perfectly fine. But as n grows, the number of candidates grows exponentially (10^n), so this becomes impractical for larger values.

Algorithm

  1. If n is 0, return 1.
  2. Initialize a counter count = 0.
  3. For each number i from 0 to 10^n - 1:

a. Check if all digits of i are unique (use a boolean array or set of size 10).

b. If all digits are unique, increment count.

  1. Return count.

Example Walkthrough

1n=2, range=[0, 100). Check each number for unique digits.
0
11
1
22
2
33
3
44
4
55
5
66
6
77
7
88
8
99
1/5

Code

The brute force works but checks every single number individually. Since we only need to count them, not find them, we can use the structure of digit positions to calculate the answer directly.

Approach 2: Dynamic Programming (Combinatorial Counting)

Intuition

Instead of checking each number, let us count directly. The key insight is to think about how many numbers of exactly k digits have all unique digits, then sum across all digit lengths from 0 to n.

For a single digit (k = 1), all 10 digits (0 through 9) are valid. That gives us 10 numbers with unique digits.

For exactly 2-digit numbers (10 through 99), the first digit has 9 choices (1 through 9, since leading zeros are not allowed). The second digit has 9 choices (0 through 9 minus whatever we picked for the first digit). So there are 9 * 9 = 81 two-digit numbers with unique digits.

For exactly 3-digit numbers, the first digit has 9 choices, the second has 9 choices, and the third has 8 choices (10 total digits minus the 2 already used). That is 9 9 8 = 648.

See the pattern? For k-digit numbers with unique digits:

  • First digit: 9 choices (cannot be 0)
  • Second digit: 9 choices (can be 0, but cannot repeat the first)
  • Third digit: 8 choices
  • Fourth digit: 7 choices
  • ...
  • k-th digit: (11 - k) choices

The total count of numbers with unique digits up to n digits is: 1 (for zero) + 9 (for 1-digit) + 99 (for 2-digit) + 99*8 (for 3-digit) + ...

Algorithm

  1. If n is 0, return 1.
  2. Start with result = 10 (covers 0-digit and 1-digit numbers).
  3. Set uniqueDigits = 9 (the count of unique k-digit numbers so far, starting with k=1 having 9 choices for the first digit).
  4. Set availableDigits = 9 (remaining digit choices for the next position).
  5. For each additional digit position from 2 to n:

a. Multiply uniqueDigits by availableDigits.

b. Add uniqueDigits to result.

c. Decrement availableDigits by 1.

  1. Return result.

Example Walkthrough

1n=3. counts[k] = unique-digit numbers with exactly k digits. 0-digit: 1 (just 0), 1-digit: 9 (1-9)
0
1
0-digit
1
9
1-digit
2
0
3
0
1/5

Code

The combinatorial approach gives us the optimal O(n) solution. But there is another way to think about this problem using backtracking, which is more flexible and extends to harder variants.

Approach 3: Backtracking

Intuition

There is another way to think about this problem that is useful for interview discussions: backtracking. We build numbers digit by digit, keeping track of which digits we have already used. At each position, we try all digits that have not been used yet.

This approach is more general and can be adapted to variants of the problem (like counting unique-digit numbers within an arbitrary range, not just [0, 10^n)). It also demonstrates recursion and backtracking skills, which interviewers value.

The idea is simple: start from an empty number and try appending each unused digit. For the first digit, we can use 1 through 9 (to avoid leading zeros for multi-digit numbers). For subsequent digits, we can use any digit from 0 through 9 that has not been used. Each valid partial number we build is counted.

Algorithm

  1. Start with count = 1 (to count the number 0).
  2. For each starting digit d from 1 to 9:

a. Mark d as used.

b. Recursively try to extend the number by appending unused digits.

c. At each recursive step, increment count and try all unused digits for the next position.

d. Stop when the number reaches n digits.

e. Unmark d (backtrack).

  1. Return count.

Example Walkthrough

1n=2, count=1 (for 0). Try first digit = 1
0
false
1
true
used
2
false
3
false
4
false
5
false
6
false
7
false
8
false
9
false
1/5

Code