Show problems as
Classic problem #2376HardMathDynamic Programming

Count Special Integers

Real-world version: Serials With No Digit Twice
Tracked in this browser only
Read Count Special Integers on LeetCode(opens in a new tab)

The 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.

Python
def count_special_numbers(n: int) -> int:
Java
public int countSpecialNumbers(int n)