HomeLeetcode357. Count Numbers with Unique Digits - Leetcode Solutions

357. Count Numbers with Unique Digits – Leetcode Solutions

Description

Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.

Examples:

Example 1:

Input: n = 2
Output: 91
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99

Example 2:

Input: n = 0
Output: 1

Solution in Python

To solve this problem, we need to count all numbers with unique digits in the range 0≤x<10n, where n is the number of digits. For example, when n=2, the range is 0≤x<100.

Key Observations:

  1. Unique Digits:
    • A number has unique digits if no digit repeats. For example, 123 is a number with unique digits, while 112 is not.
  2. Total Numbers:
    • For n=2, the range of numbers is 0≤x<100, i.e., there are 100 numbers in this range (from 0 to 99).
    • However, numbers like 11, 22, and 99 are not considered because they have repeating digits.
  3. Special Case (n = 0):
    • If n=0, we are essentially counting the number of unique-digit numbers in the range 0≤x<1, which is only the number 0. Thus, the result is 1.
  4. Counting Formula:
    • For n>0, we count unique-digit numbers by constructing numbers starting from 1 digit up to n digits.
    • For a number with 1 digit, the total count is 10 (from 0 to 9).
    • For a number with 2 digits, the first digit can be any digit from 1 to 9 (9 choices, excluding 0), and the second digit can be any digit except the first one (9 choices), leading 9×9 unique-digit numbers.
    • For 3 digits, the first digit has 9 choices, the second has 9 choices, and the third has 8 choices (to avoid repetition), leading to 9×9×8 unique-digit numbers.
    • This pattern continues for higher values of n.
  5. Summing Unique Digit Numbers:
    • We calculate the unique-digit numbers for 1-digit, 2-digit, and so on, and sum them to get the total count.
Python
class Solution:
    def countNumbersWithUniqueDigits(self, n: int) -> int:
        # If n is 0, the only number is 0, so the result is 1
        if n == 0:
            return 1

        # Initialize the total count of unique numbers
        total_count = 10  # For n = 1, we have 10 numbers (0-9)
        
        # Variable to keep track of the unique digits calculation
        unique_digits = 9  # For the first digit, we have 9 options (1-9, excluding 0)
        available_digits = 9  # For subsequent digits, we have decreasing available digits
        
        # Loop through each digit length from 2 to n
        for i in range(2, n + 1):
            unique_digits *= available_digits  # Multiply the unique digits by available options
            total_count += unique_digits  # Add the number of unique-digit numbers for i digits
            available_digits -= 1  # Decrease the available digits for the next position
        
        return total_count

Solution in C++

C++
class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        // If n is 0, the only number is 0, so return 1
        if (n == 0) return 1;
        
        // Start with 10 because for n = 1, the numbers are from 0 to 9 (inclusive)
        int uniqueCount = 10;
        int availableDigits = 9;  // To keep track of remaining digits to use
        int currentMultiplier = 9; // To select digits for places
        
        // For each n > 1, calculate the number of unique digit numbers
        for (int i = 2; i <= n && availableDigits > 0; ++i) {
            currentMultiplier *= availableDigits;
            uniqueCount += currentMultiplier;
            --availableDigits;
        }
        
        return uniqueCount;
    }
};

Solution in Javascript

JavaScript
var countNumbersWithUniqueDigits = function(n) {
    // Base case: if n is 0, the only number is 0 itself, so return 1.
    if (n === 0) {
        return 1;
    }

    // Initialize the result with the count of single-digit numbers (0 to 9).
    let result = 10; // For n = 1, there are 10 unique numbers (0-9).

    // Variable to keep track of the current count of unique digit numbers.
    let uniqueDigits = 9;  // For the first non-zero digit, we can choose 9 (1-9).
    let availableDigits = 9; // After picking the first digit, we have 9 digits left to choose.

    // Loop through the remaining digit places (from 2 digits up to n digits).
    for (let i = 2; i <= n; i++) {
        // Multiply the number of choices for this place by the remaining available digits.
        uniqueDigits = uniqueDigits * availableDigits;

        // Add the unique numbers with i digits to the result.
        result += uniqueDigits;

        // Decrease available digits for the next place.
        availableDigits--;
    }

    return result;
};

Solution in Java

Java
class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        // Base case: if n is 0, there is only one number which is 0
        if (n == 0) return 1;
        
        // Start with 10 because for n = 1 (0 <= x < 10), all digits are unique (0 to 9)
        int result = 10;
        
        // The count of unique digit numbers for numbers with more than 1 digit.
        int uniqueDigits = 9;  // The first digit can only be from 1 to 9 (9 choices)
        int availableDigits = 9; // For the remaining digits (after the first), 0-9 except the used one
        
        // Iterate from 2 to n to calculate for numbers with more digits
        for (int i = 2; i <= n; i++) {
            uniqueDigits *= availableDigits;  // Multiply by the number of available digits left
            result += uniqueDigits;  // Add to result
            availableDigits--;  // Reduce the number of available digits for the next iteration
        }
        
        return result;  // Return the final result
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular