Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-285

Count Numbers with Unique Digits

Problem

Given n, count all numbers in the range from 0 up to 10 to the n minus 1 whose decimal digits are all distinct.

The answer for length 0 is 1; for each further digit length, multiply the previous unique count by the number of still available digits and add it to the total. Once the available digit count reaches zero the total stops growing.

Hint

The leading digit has 9 choices and each later position one fewer.

Test cases

case 1
in: [0]
expected: 1
case 2
in: [1]
expected: 10
case 3
in: [2]
expected: 91
case 4
in: [3]
expected: 739
case 5
in: [4]
expected: 5275
Starter codePython
def count_unique_digits(n):
    # Your code here
    pass

5 test cases · run your code against them in the editor