MediumMathDynamic ProgrammingBacktracking

Count Numbers with Unique Digits

LeetCode
1 approach, code in all languages

Given an integer `n`, count how many integers `x` in the range `0 <= x < 10^n` have all distinct digits, meaning no digit appears more than once in `x`.

Return that count. Note that single-digit numbers, including `0`, always qualify because they contain only one digit.

Example 1

Input: n = 2

Output: 91

Every value from 0 to 99 counts except the nine repeated-digit numbers 11, 22, 33, 44, 55, 66, 77, 88, and 99, leaving 100 - 9 = 91.

Example 2

Input: n = 0

Output: 1

The only integer with 0 <= x < 10^0 = 1 is 0 itself, which trivially has unique digits.

Constraints

  • 0 <= n <= 8
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now