Numbers With Repeated Digits - Problem

Given an integer n, you need to find how many positive integers in the range [1, n] contain at least one repeated digit.

For example, the number 1223 has repeated digits (the digit '2' appears twice), while 1234 has all unique digits.

Goal: Count all numbers from 1 to n that have duplicate digits.
Input: A positive integer n
Output: The count of numbers with at least one repeated digit

Note: This is a challenging combinatorics problem that can be solved by calculating the complement - finding numbers with all unique digits and subtracting from the total.

Input & Output

example_1.py β€” Small Number
$ Input: n = 20
β€Ί Output: 1
πŸ’‘ Note: Only number 11 has repeated digits in range [1,20]. Numbers 1-10 and 12-20 all have unique digits.
example_2.py β€” Larger Number
$ Input: n = 100
β€Ί Output: 10
πŸ’‘ Note: Numbers with repeated digits: 11, 22, 33, 44, 55, 66, 77, 88, 99, 100. Total = 10 numbers.
example_3.py β€” Edge Case
$ Input: n = 1000
β€Ί Output: 262
πŸ’‘ Note: Many numbers have repeated digits: all numbers like 11, 22, ..., 99, 100, 101, 110, 111, 112, etc. The mathematical approach counts unique digits and subtracts.

Visualization

Tap to expand
πŸš— License Plate Validation: Counting Invalid PlatesValid Plates123All digits uniqueβœ“ ACCEPTEDInvalid Plates122Digit '2' repeatedβœ— REJECTEDMathematical ApproachInstead of checking each plate:1. Count valid plates using P(10,k)2. Invalid = Total - Valid⚑ O(log n) vs O(n)Example: Count Invalid Plates from 1 to 234Step 1: Count valid 1-digit plates: 1,2,3,4,5,6,7,8,9 = 9 platesStep 2: Count valid 2-digit plates: 9 Γ— 9 = 81 plates (firstβ‰ 0, secondβ‰ first)Step 3: Count valid 3-digit plates ≀ 234: Use digit-by-digit analysis = 88 platesResult: Invalid plates = 234 - (9 + 81 + 88) = 56 platesπŸ”¨ Brute Force Approachβ€’ Check each plate 1 to n individuallyβ€’ Time: O(n Γ— d) where d = digitsβ€’ Too slow for large n (10⁹)⚑ Mathematical Approachβ€’ Count valid plates using permutationsβ€’ Time: O(log n) - only process digitsβ€’ Works for any n up to 10⁹🎯 Key Insight: It's easier to count valid arrangements than invalid ones!
Understanding the Visualization
1
The Problem
Count how many license plates from 1 to n have duplicate digits (invalid plates)
2
Brute Force Method
Check each plate individually - very slow for large n
3
Smart Method
Count valid plates (unique digits) using math, then subtract from total
4
Mathematical Magic
Use permutations: P(10,k) ways to arrange k unique digits from 0-9
Key Takeaway
🎯 Key Insight: Rather than checking each number for duplicates, we use the complement principle - count numbers with unique digits using permutation mathematics, then subtract from the total. This transforms an O(n) problem into O(log n)!

Time & Space Complexity

Time Complexity
⏱️
O(log n)

We only process each digit position of n once, so time is proportional to number of digits in n

n
2n
⚑ Linearithmic
Space Complexity
O(1)

Only using a few variables for calculations, no additional data structures needed

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ n ≀ 109
  • n is a positive integer
  • The answer fits in a 32-bit signed integer
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
41.2K Views
Medium Frequency
~35 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen