2-D Dynamic Programming Digit DP
604. Count Special Integers
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
110Explanation: 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
90Explanation: 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
8877690Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 1015
Clarifying questions
What you would ask an interviewer before writing any code.