Count all numbers with unique digits in a given range
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In computer science and mathematics, counting unique-digit numbers within a specified range is a common problem that requires a well-thought-out approach. Unique-digit numbers are those numbers in which no digit is repeated. For instance, 123 and 478 are unique-digit numbers, while 112 and 343 are not. Solving this problem involves understanding permutations, combinatorics, and enumerating digits in a systematic way.
Problem Statement
Given a range `[start, end]`, the task is to count all the numbers with unique digits within this range. For example, if the range is `[100, 200]`, the unique-digit numbers include 102, 103, 104, ..., but not 101 or 110.
Technical Explanation
Combinatorial Approach
Calculating the number of unique-digit numbers relies heavily on the concept of permutations. The challenge lies in selecting digits such that no two digits are the same.
- Permutations for Unique-Digit Numbers: • For a single digit, any number from 1 to 9 (inclusive) is unique. • For two digits, choose the first digit from 1-9 and the second from the remaining nine digits. This is calculated as iterations. • For three-digit numbers, the first digit is chosen from 1-9, the second from the remaining nine, and the third from the remaining eight, giving us possibilities.
- Handling the Zero: • Zero cannot be the leading digit but can take any other position in a number after the first position.
Inclusion of Constraints
To count unique-digit numbers in a range `[start, end]`, both direct counting and digital root exploration can be fundamental. They involve:
- Direct Counting: • Enumerate numbers in the specified range and assess if they are unique-digit by converting each number to a string and checking for duplicates. • Complexity depends on range size and can be reduced by smart enumeration.
- Digital Representation: • Convert number into a bitmask for digits, verifying if the bit array doesn't contain the same bit set twice.
Example Walkthrough
Consider counting numbers with unique digits between 250 and 300:
• Brute Force: • Iterate over numbers 250 to 300. • For each number, break it into digits and employ a set to check for uniqueness. • Count numbers without duplicate digits.
Example: • 254: Unique digits (2, 5, 4) • 255: Repeating digit 5
Data Table
| Number of Digits | Permutations Available | Example Range |
| 1 | 9 | 1 to 9 |
| 2 | 81 | 10 to 99 |
| 3 | 648 | 100 to 999 |
| N | Variable limits |
Edge Cases
- Range Limits: Small ranges or those entirely bounded by numbers without unique digits should return zero.
- Single Digit Ranges: They trivially yield counts equivalent to the range size if it's between 1 to 9.
- Strict Limits: Ensure accurate count by strictly adhering to start and end inclusions. Special attention if start or end themselves have bidirectional inclusivity.
Advanced Topics
Backtracking
Instead of iterating naively, backtracking builds numbers digit by digit, choosing non-repeating digits:
• Validates partial solutions efficiently. • Reduces unnecessary calculations. • Can significantly enhance performance for large ranges.
Dynamic Programming
Consider using memoization for previously computed results within sub-ranges. This minimizes repeat evaluations:
• Breaks large problems into smaller overlapping sub-problems. • Particularly useful in contexts with complex or overlapping substructures.
Conclusion
Counting numbers with unique digits within a range is an intriguing task combining permutations with algorithm efficiency. By considering advanced techniques like backtracking and dynamic programming, it's possible to handle large ranges or constraints swiftly and effectively. This exploration ensures both an accurate count and a deeper understanding of the underlying combinatorial principles.
Related reading
- count distinct slices in an array
- Count number of 1's in binary representation
- Count number of points inside a circle fast
- Count number of subsets with sum equal to k
- Count how many matrices have full rank for all submatrices
- Count item frequency
- Count number of times each bit is set in a range of integers
- Count Number of Triples in an array that are collinear

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.