oneby7

2-D Dynamic Programming Digit DP

604. Count Special Integers

Hard

We call a positive integer special if no digit appears in it twice. For example, 3051 and 7 are special, but 2020 and 11 are not.

Given a positive integer n, return how many special integers there are from 1 to n, including n itself.

Numbers are written the usual way, with no leading zeros. So 7 is the one digit 7, not 007.

Example 1

Input: n = 135

Output: 110

Explanation: All 9 one-digit numbers are special. So are the two-digit numbers except 11, 22, …, 99, which makes 81 more. From 100 to 135, 20 numbers are special, such as 102, 120 and 135 itself. That makes 9 + 81 + 20 = 110.

Example 2

Input: n = 100

Output: 90

Explanation: The 9 one-digit numbers and 81 two-digit numbers are special. 100 itself repeats the digit 0, so it does not count.

Example 3

Input: n = 1000000000000000

Output: 8877690

Check your understanding

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

n = 20

Constraints

  • 1 ≤ n ≤ 1015

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.