AlgoMaster Logo

Count Numbers with Unique Digits

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force Approach

In a brute force approach, we can generate all numbers with up to n digits and count those with unique digits. However, this method will be computationally expensive due to the vast number of possibilities, making it less suitable for larger values of n.

Intuition:

  1. Generate all numbers with up to n digits.
  2. Check each number to see if it contains unique digits.
  3. Count those numbers.

Code:

2. Mathematical Combinatorics Approach

Instead of generating each number, we can use combinatorial counting to determine how many numbers have unique digits.

Intuition:

  • For a given n:
    • The first digit has 9 options (1 to 9), as 0 cannot be the first digit.
    • The second digit has 9 options (0 to 9 except the first digit).
    • The third digit has 8 options, and so on.
  • Use combinatorial multiplication to compute the count of numbers with unique digits.

Code: