Count all numbers with unique digits in a given range
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

