oneby7

2-D Dynamic Programming Digit DP

602. Count Numbers with Unique Digits

Medium

Given an integer n, return how many whole numbers x with 0 ≤ x < 10n have no digit that appears twice.

Numbers are written the usual way, with no leading zeros. So 7 is the one digit 7, not 007, and 0 is the one digit 0. For example, 3051 counts, because its four digits are all different, but 2020 and 11 do not.

Example 1

Input: n = 3

Output: 739

Explanation: The numbers below 1000 are counted. All 10 one-digit numbers count. Of the two-digit numbers, 81 count (all but 11, 22, …, 99), and 648 three-digit numbers count. That makes 10 + 81 + 648 = 739.

Example 2

Input: n = 0

Output: 1

Explanation: The only number below 100 = 1 is 0, and its one digit does not repeat.

Example 3

Input: n = 1000000000

Output: 8877691

Check your understanding

Work out the answer for this input before you write any code.

n = 2

Constraints

  • 0 ≤ n ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.