Skip to the code
cf.
+50 XP

#562 Count IDs With Unique Digits

hard

A support system gives each ticket an ID from 1 upward. To make IDs easy to read aloud, a team wants to know how many IDs from 1 to n have no repeated digit (for example 1234 qualifies, 1213 does not).

Write countUniqueDigitIds(n) that returns how many integers from 1 to n (inclusive) have all different digits.

Example 1

Input: n = 20
Output: 19

Every number from 1 to 20 except 11.

Example 2

Input: n = 100
Output: 90

1 to 9 (9 numbers), plus the 81 two-digit numbers without a repeat. 100 repeats 0.

Constraints

  • 1 <= n <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).