Count Special Integers
Real-world version: Serials With No Digit TwiceThe statement here is our own: the same inputs, outputs and technique.
Serials are stamped as plain whole numbers with no leading zeros.
A serial is clean when no digit appears in it more than once.
Given n, return how many clean serials there are from 1 up to n inclusive.
Examples
Example 1
- Input
- n = 4321
- Output
- 2434
Every serial of one, two or three digits with no repeat counts, which is 9 plus 81 plus 648, and then the four-digit serials up to 4321 with no repeat add a further 1080.
Example 2
- Input
- n = 101
- Output
- 90
The nine single-digit serials are all clean, and of the ninety two-digit serials only the nine doubled ones are not. Neither 100 nor 101 is clean, so the count stops at 90.
Example 3
- Input
- n = 9
- Output
- 9
A single digit cannot repeat, so all nine serials are clean.
Constraints
- 1 <= n <= 2 * 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def count_special_numbers(n: int) -> int:public int countSpecialNumbers(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's