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 is repeated in the number. For example, 123 has unique digits but 121 does not.

Input & Output

Example 1 — Small Case
$ Input: n = 2
Output: 91
💡 Note: Count all numbers from 0 to 99 with unique digits: single digits (0-9) = 10, two-digits with unique digits (10,12,13,...,98) = 81. Total = 10 + 81 = 91
Example 2 — Minimal Case
$ Input: n = 0
Output: 1
💡 Note: Range is [0, 1), so only number 0 exists, which has unique digits
Example 3 — Larger Case
$ Input: n = 3
Output: 739
💡 Note: Numbers 0 to 999: 1-digit = 10, 2-digit = 81, 3-digit = 648. Total = 10 + 81 + 648 = 739

Constraints

  • 0 ≤ n ≤ 8

Visualization

Tap to expand
Count Numbers with Unique Digits (n=2)Input: n = 2Range: [0, 100)Find unique digitsCount by Length1-digit: 0,1,2...92-digit: 10,12,13...Result9110 + 81 = 9110Unique ✓12Unique ✓11Duplicate ✗23Unique ✓22Duplicate ✗Mathematical Formula: 9×9 = 81 two-digit unique numbersFirst digit: 9 choices (1-9), Second digit: 9 choices (0 + 8 remaining)Total: 10 (1-digit) + 81 (2-digit) = 91
Understanding the Visualization
1
Input
Given n=2, find count in range [0, 100)
2
Process
Count by digit length: 1-digit + 2-digit numbers
3
Output
Total count of unique digit numbers
Key Takeaway
🎯 Key Insight: Use permutation formula P(10,k) to count directly instead of checking every number
Asked in
Google 25 Facebook 18
23.0K 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