Show problems as
Classic problem #357MediumMathDynamic ProgrammingBacktracking

Count Numbers with Unique Digits

Real-world version: Clean Crate Stamps
Tracked in this browser only
Read Count Numbers with Unique Digits on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstCounting Problems · Dynamic Programming, animated step by step

A depot stamps its crates with the whole numbers from 0 up to but not including 10^n. A stamp is clean when no digit occurs twice in it, written the ordinary way with no leading zeros: 9, 40 and 907 are clean, while 11, 121 and 900 are not.

Return how many stamps in that range are clean. When n is 0 the range holds the single number 0.

Examples

Example 1

Input
n = 1
Output
10

The stamps run from 0 through 9. Each is a single digit, so none of them repeats a digit and all ten are clean.

Example 2

Input
n = 3
Output
739

The stamps run from 0 through 999, and 739 of them use no digit twice. For instance 5, 40 and 907 count, while 11, 121 and 900 do not.

Example 3

Input
n = 5
Output
32491

The stamps run from 0 through 99999, and 32491 of them use no digit twice. For instance 13579 counts, while 13577 does not.

Constraints

  • 0 <= n <= 8

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_clean_stamps(n: int) -> int:
Java
public int countCleanStamps(int n)