Count Numbers with Unique Digits - Problem

Given an integer n, return the count of all numbers with unique digits, x, where 0 ≤ x < 10n.

A number has unique digits if no digit appears more than once. For example, 123 has unique digits, but 112 does not because the digit 1 appears twice.

Examples:

  • When n = 2, we need to count numbers from 0 to 99 with unique digits
  • Numbers like 0, 1, 2, ..., 9, 10, 12, 13, ..., 98 are valid (unique digits)
  • Numbers like 11, 22, 33, 44, 55, 66, 77, 88, 99 are invalid (repeated digits)

This problem combines combinatorics and dynamic programming concepts to efficiently count valid numbers without generating them all.

Input & Output

example_1.py — Basic Case
$ Input: n = 2
Output: 91
💡 Note: For n=2, we count numbers 0-99. Single digits (0-9): 10 numbers. Two-digit numbers with unique digits: 9×9=81 (first digit 1-9, second digit can be 0-9 except first). Total: 10 + 81 = 91.
example_2.py — Minimum Case
$ Input: n = 0
Output: 1
💡 Note: For n=0, the range is [0, 10^0) = [0, 1), which contains only the number 0. Since 0 has unique digits (trivially), the answer is 1.
example_3.py — Edge Case
$ Input: n = 1
Output: 10
💡 Note: For n=1, we count numbers 0-9. All single-digit numbers have unique digits by definition. So the answer is 10.

Visualization

Tap to expand
🚗 License Plate Factory: Smart Counting vs Brute Force❌ Brute Force FactoryMakes every possible plate:000102...1112Checks 100 plates for n=2Time: O(n × 10^n)✅ Smart Mathematical FactoryCalculates without making plates:1-digit: 10(0,1,2,...,9)2-digit: 9×9=81(10,12,13,...)Formula: First digit (1-9): 9 choicesSecond digit (0-9 except first): 9 choicesTotal: 10 + 81 = 91Time: O(n) - Just 2 calculations!📊 Efficiency ComparisonFor n=2 (numbers 0-99):Brute Force: 100 checksMathematical: 2 calculations50x faster!🎯 Key Insight:Instead of generating and checking every number, use permutation formulas to count valid combinations directly!
Understanding the Visualization
1
Count by length
Separate plates by number of digits (1-digit, 2-digit, etc.)
2
Apply constraints
First position: 9 choices (1-9), can't start with 0
3
Permutation math
Each subsequent position has one fewer choice
4
Sum all cases
Add up valid combinations for all digit lengths
Key Takeaway
🎯 Key Insight: Mathematical counting using permutations is exponentially faster than brute force generation and checking.

Time & Space Complexity

Time Complexity
⏱️
O(n)

We calculate the result for each digit length from 1 to n, each taking O(1) time

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables to store intermediate calculations

n
2n
Linear Space

Constraints

  • 0 ≤ n ≤ 8
  • For n > 10, the answer would be the same as n = 10 since we only have 10 unique digits (0-9)
  • Note: The maximum meaningful value is n = 10
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen