Count Special Integers - Problem

Imagine you're working at a digital security company that needs to identify "special" numbers for encryption purposes. A positive integer is considered special if all of its digits are distinct (no repeated digits).

For example:

  • 123 is special ✅ (all digits 1, 2, 3 are different)
  • 1001 is NOT special ❌ (digit 1 appears twice, digit 0 appears twice)
  • 987654321 is special ✅ (all 9 digits are unique)

Given a positive integer n, your task is to count how many special integers exist in the range [1, n] inclusive.

Goal: Return the count of special integers from 1 to n.
Input: A positive integer n
Output: Number of special integers in range [1, n]

Input & Output

example_1.py — Basic Case
$ Input: n = 20
Output: 19
💡 Note: All numbers from 1 to 20 have unique digits except 11. Numbers 1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20 are special (19 total).
example_2.py — Larger Case
$ Input: n = 100
Output: 91
💡 Note: All 1-digit numbers (1-9) are special: 9 numbers. For 2-digit numbers (10-99), we exclude those with repeated digits: 11,22,33,44,55,66,77,88,99. So 90-9=81 two-digit special numbers. Total: 9+81+1=91 (including 100 which is NOT special due to repeated 0s, so actually 9+81=90, plus 100 is not special, so 90).
example_3.py — Edge Case
$ Input: n = 1000
Output: 738
💡 Note: Includes all 1-digit (9), valid 2-digit (81), valid 3-digit numbers, but 1000 itself has repeated 0s so is not special.

Constraints

  • 1 ≤ n ≤ 2 × 109
  • n is a positive integer
  • Special integer: All digits in the number are distinct

Visualization

Tap to expand
🎯 License Plate Factory: Counting Special Numbers1-Digit Plates🚗 7All unique: 9 total2-Digit Plates🚙 349×9=81 valid3-Digit Plates🚐 5679×9×8=648Invalid Plates🚫 1001Repeated digitsFactory Assembly Line Process:1Count all 1-digit special numbers (1-9)2For 2-digit: first digit (1-9) × second digit (0-9 except first)3For k-digit: use permutation P(10,k) with first digit ≠ 04Use digit DP for boundary case (same length as n)🎯 Key InsightInstead of checking every license plate individually,we calculate mathematically how many validcombinations exist for each length category!Time: O(d²) instead of O(n×d)
Understanding the Visualization
1
Count by Length
First count all valid 1-digit plates (1-9), then 2-digit plates, etc.
2
Use Combinatorics
For k-digit plates: first position has 9 choices (1-9), second has 9 choices (0-9 except first), third has 8 choices, etc.
3
Handle Boundaries
Use digit DP to carefully count numbers with same length as n but ≤ n
4
Combine Results
Sum up counts from all different lengths to get final answer
Key Takeaway
🎯 Key Insight: Mathematical counting using digit DP is exponentially faster than checking each number individually, making the solution scalable for large inputs.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
47.5K Views
Medium Frequency
~25 min Avg. Time
1.4K 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