Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Count Numbers with Unique Digits problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Count Numbers with Unique Digits problem solution you have given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.

Leetcode Count Numbers with Unique Digits problem solution

Leetcode Count Numbers with Unique Digits problem solution in Python.

class Solution:
    def countNumbersWithUniqueDigits(self, n):
        if n == 0:
            return 1
        
        if n == 1:
            return 10
        
        if n == 2:
            return 91
        
        i = 3
        total = 91
        k = 9*9
        
        while i <= n:
            total += k * (11 - i)
            k *= (11 - i)
            i += 1
            
            if i == 11:
                break
        
        return total

Count Numbers with Unique Digits problem solution in Java.

class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        if(n==0){
            return 1;
        }
        int sum = 0;
        for(int k=1;k<=n;k++){
            sum += count(k);
        }
        return sum+1;
    }
    
    public int count(int n){
        int product = 9;        
        for(int i=0;i<n-1;i++){
            product = product * (9-i);
        }
        return product;
    }
}

Problem solution in C++.

int countNumbersWithUniqueDigits(int n) {
        int s = 0;
        int p = 1;
        for (int i=1; i<=min(10, n); i++) {
            s += p;
            p *= 10 - i;
        }
        return s * 9 + 1;
    }

Problem solution in C.

int fac(int a, int b){
    int result = 1;
    while(a > b){
        result *= a;
        a--;
    }
    return result;
}
int countNumbersWithUniqueDigits(int n) {
    if(n == 1 || n == 0) return pow(10, n);
    if(n > 10) return countNumbersWithUniqueDigits(10);
    
    int result = 0;
    result = countNumbersWithUniqueDigits(n - 1) + fac(9, 9-n) + fac(9, 9 - n + 1) * (n - 1);
    return result;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes