All problems
0898HardMathDynamic Programming

Serials With No Digit Twice

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2376Count Special Integers

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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