Count Numbers With Unique Digits II - Problem
Given two positive integers
For example, the number
Goal: Return the count of numbers with unique digits in the given range.
Input: Two positive integers
Output: An integer representing the count of numbers with unique digits in range
a and b, your task is to count how many numbers in the range [a, b] (inclusive) have unique digits. A number has unique digits if no digit appears more than once in its decimal representation.For example, the number
123 has unique digits because each digit (1, 2, 3) appears only once. However, 1223 does not have unique digits because the digit 2 appears twice.Goal: Return the count of numbers with unique digits in the given range.
Input: Two positive integers
a and b where a โค bOutput: An integer representing the count of numbers with unique digits in range
[a, b] Input & Output
example_1.py โ Basic Range
$
Input:
a = 1, b = 20
โบ
Output:
19
๐ก Note:
All numbers from 1-20 have unique digits except 11 (digit 1 repeats). Numbers with unique digits: 1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20.
example_2.py โ Small Range
$
Input:
a = 80, b = 120
โบ
Output:
27
๐ก Note:
In range [80,120], numbers like 88, 99, 100, 110, 111, etc. have repeated digits. Only numbers with all unique digits are counted.
example_3.py โ Single Number
$
Input:
a = 123, b = 123
โบ
Output:
1
๐ก Note:
Only checking number 123, which has digits 1, 2, 3 - all unique, so count is 1.
Constraints
- 1 โค a โค b โค 108
- The range [a, b] will contain at most 106 numbers
- Time limit: 2 seconds
Visualization
Tap to expand
Understanding the Visualization
1
Number Processing
Each number passes through our digit scanner
2
Digit Extraction
Scanner extracts each digit one by one
3
Duplicate Detection
Boolean array tracks which digits we've seen
4
Immediate Rejection
If duplicate found, reject immediately
5
Count Valid
Only numbers with unique digits pass through
Key Takeaway
๐ฏ Key Insight: Using a boolean array to track seen digits allows O(1) duplicate detection per digit, making the algorithm efficient while processing each number in the range.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code