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:
123is special ✅ (all digits 1, 2, 3 are different)1001is NOT special ❌ (digit 1 appears twice, digit 0 appears twice)987654321is 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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code