Talentd
FleetCode

1012. Numbers With Repeated Digits

Hard48.9% acceptance2 companies

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

Example 1:

Input: n = 20
Output: 1
Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.

Example 2:

Input: n = 100
Output: 10
Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.

Example 3:

Input: n = 1000
Output: 262

Constraints:

  • 1 <= n <= 109

Asked at 2 companies

Hints open one at a time

Hint 1
How many numbers with no duplicate digits? How many numbers with K digits and no duplicates?
Hint 2
How many numbers with same length as N? How many numbers with same prefix as N?
19:32
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def numDupDigitsAtMostN(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
20
Input
100
Input
1000
Your code is saved on this device.Solution