855. All-Different Digits
A parking garage prints ticket numbers, and a number is called crisp if no digit appears twice in its decimal form: 4096 and 58 are crisp, 1221 and 100 are not. Given a positive integer n, return how many crisp integers lie in the range [1, n].
Because n can be as large as one billion, checking each number separately would take far too long. Think about how many choices each digit position leaves once earlier digits have been used, and use the digits of n to decide where the counted numbers first fall below it.
Example 1
- Input:
- n = 57
- Output:
- 52
- Explanation:
There are 9 one-digit numbers and, among 10 to 57 (48 numbers), all except 11, 22, 33, 44 and 55 qualify: 43. In total 9 + 43 = 52.
Example 2
- Input:
- n = 130
- Output:
- 107
- Explanation:
Numbers 1 to 99 give 9 + 81 = 90 crisp numbers; from 100 to 130 only 102 to 109, 120, 123 to 129 and 130 qualify (17 numbers), so the total is 107.
Constraints
- 1 ≤ n ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms
Expected complexity
- Time
- O(D^2)
- Space
- O(D)