Count Numbers with Unique Digits - Medium - NL

less than 1 minute read

Published:

Question

  • https://leetcode.com/problems/count-numbers-with-unique-digits/

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

Approach

  • Count

Solution

class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        if(n==0) {
            return 1;
        }
        int ans = 10;
        for(int i=2;i<=n;i++) {
            int temp = 0;
            temp = 9*9;
            int y = 8;
            for(int j=3;j<=i;j++) {
                temp = temp*y;
                y--;
            }
            ans += temp;
        }
        return ans;
    }
};