Distinct Digit Codes

Easy1-D Dynamic Programming

Problem

A locker system issues numeric codes x with 0 ≤ x < 10^n, written without leading zeros (so 7 is the code "7", not "007").

Return how many of these codes have no repeated digit.

Example 1

Input:
n = 2
Output:
91

All 100 values 0–99 except the 9 with a repeated digit: 11, 22, ..., 99.

Example 2

Input:
n = 0
Output:
1

Only x = 0.

Constraints

  • 0 ≤ n ≤ 10

Hint

Show the first hint

Group the codes by their number of digits k and count each group separately.

Walkthrough

How to approach it

01Clarify before coding

  • Is 0 a valid code? Yes, and it has distinct digits.
  • Can n exceed 10? Not here; beyond 10 digits a repeat is forced, so the answer stops growing.
  • Are leading zeros allowed? No: each value is written in its normal form.

02Brute force

Loop over all values from 0 to 10^n - 1 and test each with a set of its digits.

Cost: O(10^n · n): 10^11 digit checks for n = 10.

The rest of the walkthrough is in the app

The full walkthrough (key insight, step-by-step, dry run, complexity, edge cases, pitfalls, follow-ups) and solutions in Python and JavaScript are in the app, next to an editor and a terminal that runs your code against the tests.

This problem is part of Pro.