Count Numbers With Unique Digits II - Problem

Given two positive integers a and b, return the count of numbers having unique digits in the range [a, b] (inclusive).

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

Input & Output

Example 1 — Small Range
$ Input: a = 20, b = 25
Output: 5
💡 Note: Numbers 20, 21, 23, 24, 25 all have unique digits. Number 22 has repeated digit '2', so it's not counted.
Example 2 — Single Digits
$ Input: a = 1, b = 10
Output: 10
💡 Note: All single digit numbers (1-9) and 10 have unique digits. Total count is 10.
Example 3 — Range with Repeats
$ Input: a = 80, b = 120
Output: 27
💡 Note: Count numbers from 80-120 where all digits are unique. Excludes numbers like 88, 99, 100, 101, 110, 111, etc.

Constraints

  • 1 ≤ a ≤ b ≤ 108
  • Both a and b are positive integers

Visualization

Tap to expand
Count Numbers With Unique Digits: Range [20, 25]Input: a = 20, b = 252021222324252≠0 ✓2≠1 ✓2=2 ✗2≠3 ✓2≠4 ✓2≠5 ✓Check: Green = unique digits, Red = repeated digitsValid numbers: 20, 21, 23, 24, 25Output5
Understanding the Visualization
1
Input Range
Given range [20, 25] to check
2
Check Uniqueness
Test each number for repeated digits
3
Count Valid
Return count of numbers with unique digits
Key Takeaway
🎯 Key Insight: Use hash sets for O(1) digit duplicate detection instead of nested loops
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
23.5K Views
Medium Frequency
~15 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