Talentd
FleetCode

2376. Count Special Integers

Hard45.2% acceptance1 company

We call a positive integer special if all of its digits are distinct.

Given a positive integer n, return the number of special integers that belong to the interval [1, n].

Example 1:

Input: n = 20
Output: 19
Explanation: All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.

Example 2:

Input: n = 5
Output: 5
Explanation: All the integers from 1 to 5 are special.

Example 3:

Input: n = 135
Output: 110
Explanation: There are 110 integers from 1 to 135 that are special.
Some of the integers that are not special are: 22, 114, and 131.

Constraints:

  • 1 <= n <= 2 * 109

Asked at 1 company

Hints open one at a time

Hint 1
Try to think of dynamic programming.
Hint 2
Use the idea of digit dynamic programming to build the numbers, in addition to a bitmask that will tell which digits you have used so far on the number that you are building.
19:55
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countSpecialNumbers(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
20
Input
5
Input
135
Your code is saved on this device.Solution