Count Special Integers - Problem

We call a positive integer special if all of its digits are distinct.

Given a positive integer n, return the number of special integers that belong to the interval [1, n].

A special integer has no repeated digits. For example, 123 is special, but 112 is not special because digit 1 appears twice.

Input & Output

Example 1 — Small Range
$ Input: n = 20
Output: 18
💡 Note: Special integers from 1 to 20: 1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20. We exclude 11 because digit 1 repeats. All other numbers have distinct digits.
Example 2 — Single Digit
$ Input: n = 5
Output: 5
💡 Note: All numbers from 1 to 5 have distinct digits: 1,2,3,4,5. Each single-digit number is automatically special.
Example 3 — Edge Case
$ Input: n = 100
Output: 90
💡 Note: Includes all 1-digit (9) and 2-digit numbers with distinct digits (9×9=81), but excludes 100 because it has repeated 0s. Total: 9 + 81 = 90.

Constraints

  • 1 ≤ n ≤ 2 × 109

Visualization

Tap to expand
Count Special Integers: Find numbers with distinct digitsExample: n = 20, count special integers in [1, 20]112111320...Special: All digits are different | Not Special: Some digits repeatCount = 18Numbers 1-20 with distinct digits(excludes 11)Result: 18 special integers found in range [1, 20]
Understanding the Visualization
1
Input Range
Given range [1, n], need to count special integers
2
Special Check
A number is special if no digit repeats
3
Count Result
Return total count of special integers
Key Takeaway
🎯 Key Insight: Use mathematical patterns or digit DP to count efficiently instead of checking each number individually
Asked in
Google 15 Amazon 8 Microsoft 6
23.5K Views
Medium Frequency
~35 min Avg. Time
890 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