Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Counting n digit Numbers with all unique digits in JavaScript
We need to write a JavaScript function that counts all n-digit numbers where every digit appears exactly once (all digits are unique).
Problem Statement
Given a number num, find how many numbers exist with exactly num digits where all digits are unique.
For example:
- 1-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ? Count = 10
- 2-digit numbers: 10, 12, 13, ..., 98 (excluding 11, 22, 33, etc.) ? Count = 91
Understanding the Logic
This is a combinatorics problem:
- 1-digit: All 10 digits (0-9) are valid
- 2-digit: First digit: 9 choices (1-9, not 0), Second digit: 9 choices (remaining digits including 0)
- 3-digit: First digit: 9 choices, Second: 9 choices, Third: 8 choices
Solution Using Dynamic Programming
const uniqueDigits = (num = 1) => {
// Base cases: dp[i] = count of i-digit numbers with unique digits
const dp = [1, 10];
// For n >= 2, apply the formula
for (let i = 2; i <= num; i++) {
// First digit: 9 choices (1-9)
// Remaining digits: decreasing choices (9, 8, 7, ...)
dp[i] = 9;
let availableDigits = 9;
for (let j = 1; j < i; j++) {
dp[i] *= availableDigits;
availableDigits--;
}
}
return dp[num];
};
// Test cases
console.log("1-digit numbers:", uniqueDigits(1));
console.log("2-digit numbers:", uniqueDigits(2));
console.log("3-digit numbers:", uniqueDigits(3));
console.log("4-digit numbers:", uniqueDigits(4));
1-digit numbers: 10 2-digit numbers: 81 3-digit numbers: 648 4-digit numbers: 4536
Alternative Mathematical Approach
const uniqueDigitsMath = (num) => {
if (num === 0) return 1;
if (num === 1) return 10;
if (num > 10) return 0; // Can't have more than 10 unique digits
// For n-digit numbers: 9 × 9 × 8 × 7 × ... × (11-n)
let result = 9; // First digit choices (1-9)
for (let i = 2; i <= num; i++) {
result *= (11 - i); // Remaining digit choices
}
return result;
};
// Comparison
console.log("Mathematical approach:");
console.log("1-digit:", uniqueDigitsMath(1));
console.log("2-digit:", uniqueDigitsMath(2));
console.log("3-digit:", uniqueDigitsMath(3));
console.log("Edge case - 11 digits:", uniqueDigitsMath(11));
Mathematical approach: 1-digit: 10 2-digit: 81 3-digit: 648 Edge case - 11 digits: 0
Step-by-Step Breakdown
| Digits (n) | First Digit Choices | Remaining Positions | Formula | Result |
|---|---|---|---|---|
| 1 | 10 (0-9) | - | 10 | 10 |
| 2 | 9 (1-9) | 9 (remaining digits) | 9 × 9 | 81 |
| 3 | 9 (1-9) | 9 × 8 | 9 × 9 × 8 | 648 |
| 4 | 9 (1-9) | 9 × 8 × 7 | 9 × 9 × 8 × 7 | 4536 |
Key Points
- Maximum possible digits with unique digits is 10 (using all digits 0-9)
- For 1-digit numbers, we include 0 as a valid number
- For multi-digit numbers, the first digit cannot be 0
- Each subsequent position has one fewer choice than the previous
Conclusion
This problem uses combinatorics principles where we calculate permutations with constraints. The mathematical approach is more efficient than dynamic programming for this specific problem since it directly computes the result using the formula 9 × 9! / (10-n)!.
Advertisements
