KatInterview

357. Count Numbers with Unique Digits

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to count all numbers x with unique digits, where 0 <= x < 10^n. This means we are looking for numbers that do not have any repeating digits within their decimal representation, and these numbers can have a length from 1 up to n.

Solution

Count Numbers with Unique Digits in Python

class Solution:
    def countNumbersWithUniqueDigits(self, n: int) -> int:
        # Base case: n = 0, only one number (0) in the range 0 <= x < 1.
        if n == 0:
            return 1
        
        # For n=1, result is 10 (numbers 0-9). This covers f(1).
        result = 10
        
        # unique_digits_for_length_k stores f(k), the count for numbers of length k.
        # We start by calculating f(2).
        # For the first digit of a 2-digit number, there are 9 choices (1-9).
        unique_digits_for_length_k = 9
        
        # available_choices for subsequent digits.
        # For a 2-digit number, the second digit has 9 choices (0-9, excluding the first digit).
        available_choices = 9
        
        # Iterate for lengths k from 2 up to n.
        # The constraint n <= 8 means we don't need to check k > 10.
        for k in range(2, n + 1):
            # Calculate f(k) = 9 * 9 * 8 * ...
            # For k=2, unique_digits_for_length_k = 9 * 9
            # For k=3, unique_digits_for_length_k = (9 * 9) * 8
            unique_digits_for_length_k *= available_choices
            result += unique_digits_for_length_k
            
            # Decrement the number of available choices for the next digit.
            available_choices -= 1
            
        return result

Count Numbers with Unique Digits in Java

class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        // Base case: n = 0, only one number (0) in the range 0 <= x < 1.
        if (n == 0) {
            return 1;
        }

        // For n=1, numbers are 0,1,2,3,4,5,6,7,8,9. All 10 have unique digits.
        // This is our initial result, covering numbers of length 1 (f(1)).
        int result = 10;
        
        // This variable will store the count of unique digit numbers of the current length k.
        // We start by calculating f(2). For the first digit of a 2-digit number, there are 9 choices (1-9).
        int uniqueDigitsForLengthK = 9;
        
        // Number of available choices for subsequent digits.
        // For a 2-digit number, the second digit has 9 choices (0-9, excluding the first digit).
        int availableChoices = 9;

        // Iterate for lengths from 2 up to n.
        // The constraint n <= 8 means we don't need to check for k > 10.
        for (int k = 2; k <= n; k++) {
            // Calculate f(k) = 9 * 9 * 8 * ...
            // For k=2, uniqueDigitsForLengthK = 9 * 9
            // For k=3, uniqueDigitsForLengthK = (9 * 9) * 8
            uniqueDigitsForLengthK *= availableChoices;
            
            // Add the count for the current length to the total result.
            result += uniqueDigitsForLengthK;
            
            // Decrement the number of available choices for the next digit.
            availableChoices--;
        }
        
        return result;
    }
}

Count Numbers with Unique Digits in C++

class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        // Base case: n = 0, only one number (0) in the range 0 <= x < 1.
        if (n == 0) {
            return 1;
        }

        // For n=1, numbers are 0,1,2,3,4,5,6,7,8,9. All 10 have unique digits.
        // This is our initial result, covering numbers of length 1 (f(1)).
        int result = 10;
        
        // This variable will store the count of unique digit numbers of the current length k.
        // We start by calculating f(2). For the first digit of a 2-digit number, there are 9 choices (1-9).
        int uniqueDigitsForLengthK = 9;
        
        // Number of available choices for subsequent digits.
        // For a 2-digit number, the second digit has 9 choices (0-9, excluding the first digit).
        int availableChoices = 9;
        
        // Iterate for lengths from 2 up to n.
        // The constraint n <= 8 means we don't need to check for k > 10.
        for (int k = 2; k <= n; ++k) {
            // Calculate f(k) = 9 * 9 * 8 * ...
            // For k=2, uniqueDigitsForLengthK = 9 * 9
            // For k=3, uniqueDigitsForLengthK = (9 * 9) * 8
            uniqueDigitsForLengthK *= availableChoices;
            
            // Add the count for the current length to the total result.
            result += uniqueDigitsForLengthK;
            
            // Decrement the number of available choices for the next digit.
            availableChoices--;
        }
        
        return result;
    }
};

Count Numbers with Unique Digits in TypeScript

function countNumbersWithUniqueDigits(n: number): number {
    // Base case: n = 0, only one number (0) in the range 0 <= x < 1.
    if (n === 0) {
        return 1;
    }

    // For n=1, numbers are 0,1,2,3,4,5,6,7,8,9. All 10 have unique digits.
    // This is our initial result, covering numbers of length 1 (f(1)).
    let result = 10;
    
    // This variable will store the count of unique digit numbers of the current length k.
    // We start by calculating f(2). For the first digit of a 2-digit number, there are 9 choices (1-9).
    let uniqueDigitsForLengthK = 9;
    
    // Number of available choices for subsequent digits.
    // For a 2-digit number, the second digit has 9 choices (0-9, excluding the first digit).
    let availableChoices = 9;

    // Iterate for lengths from 2 up to n.
    // The constraint n <= 8 means we don't need to check for k > 10.
    for (let k = 2; k <= n; k++) {
        // Calculate f(k) = 9 * 9 * 8 * ...
        // For k=2, uniqueDigitsForLengthK = 9 * 9
        // For k=3, uniqueDigitsForLengthK = (9 * 9) * 8
        uniqueDigitsForLengthK *= availableChoices;
        
        // Add the count for the current length to the total result.
        result += uniqueDigitsForLengthK;
        
        // Decrement the number of available choices for the next digit.
        availableChoices--;
    }
    
    return result;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.