Numbers With Repeated Digits - Problem

Given an integer n, return the number of positive integers in the range [1, n] that have at least one repeated digit.

For example, if n = 100, numbers like 11, 22, 33, 44, 55, 66, 77, 88, 99, 100 all have repeated digits.

Input & Output

Example 1 — Small Number
$ Input: n = 20
Output: 1
💡 Note: Only number 11 has repeated digits in range [1, 20]
Example 2 — Medium Number
$ Input: n = 100
Output: 10
💡 Note: Numbers 11, 22, 33, 44, 55, 66, 77, 88, 99, 100 all have repeated digits
Example 3 — Single Digit
$ Input: n = 5
Output: 0
💡 Note: Numbers 1, 2, 3, 4, 5 all have unique digits (single digit numbers)

Constraints

  • 1 ≤ n ≤ 109

Visualization

Tap to expand
Numbers With Repeated Digits: Range [1, 100]Inputn = 100Numbers with Repeated Digits11: digits 1,1 (repeat)22: digits 2,2 (repeat)33, 44, 55, 66, 77, 88, 99100: digits 1,0,0 (repeat)OutputCount = 10Strategy: Count Unique Digits, Then SubtractUnique digit numbers: 90With repeated digits: 100 - 90 = 10✓ Efficiently solved using mathematical approach
Understanding the Visualization
1
Input
Given integer n = 100
2
Process
Count numbers [1,100] with repeated digits
3
Output
Return count = 10
Key Takeaway
🎯 Key Insight: Count numbers without repeated digits using combinatorics, then subtract from total
Asked in
Google 15 Amazon 8 Microsoft 5
25.0K Views
Medium Frequency
~35 min Avg. Time
850 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