860. Repeated Digit Count

A registry issues identification numbers in the ordinary decimal form, without leading zeros, starting at 1. An auditor considers a number suspicious if at least one digit appears two or more times in it (for example 101 or 2552 are suspicious, while 407 is not).

Given n, return how many integers in the inclusive range [1, n] are suspicious. Counting them one by one is too slow when n is as large as one billion, so think about counting the numbers whose digits are all different and subtracting them from n.

Example 1

Input:
n = 55
Output:
5
Explanation:

The numbers up to 55 with a repeated digit are 11, 22, 33, 44 and 55, so the answer is 5.

Example 2

Input:
n = 130
Output:
23
Explanation:

Among 1..130 the repeated-digit numbers are 23 in total: the nine pairs 11 through 99 plus 14 numbers in 100..130 such as 100, 101 and 110.

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) where d = number of digits
Space
O(d)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…