MediumMathDynamic ProgrammingBacktracking

1 approach, code in every language

Problem description

Given an integer `n`, count how many integers `x` in the range `0 <= x < 10^n` have all distinct digits, meaning no digit appears more than once in `x`.

Return that count. Note that single-digit numbers, including `0`, always qualify because they contain only one digit.

Example 1

Input: n = 2

Output: 91

Every value from 0 to 99 counts except the nine repeated-digit numbers 11, 22, 33, 44, 55, 66, 77, 88, and 99, leaving 100 - 9 = 91.

Example 2

Input: n = 0

Output: 1

The only integer with 0 <= x < 10^0 = 1 is 0 itself, which trivially has unique digits.

Constraints

  • 0 <= n <= 8

After the pattern

Now solve one you haven't seen.

Clean Crate Stamps is our own problem on the technique behind Count Numbers with Unique Digits: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free