-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2376-count-special-integers.js
More file actions
47 lines (42 loc) · 1.07 KB
/
2376-count-special-integers.js
File metadata and controls
47 lines (42 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 2376. Count Special Integers
* https://leetcode.com/problems/count-special-integers/
* Difficulty: Hard
*
* 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].
*/
/**
* @param {number} n
* @return {number}
*/
var countSpecialNumbers = function(n) {
const digits = String(n).split('').map(Number);
const len = digits.length;
let total = 0;
for (let i = 1; i < len; i++) {
let count = 9;
for (let j = 0; j < i - 1; j++) {
count *= (10 - j - 1);
}
total += count;
}
const used = new Set();
for (let i = 0; i < len; i++) {
for (let d = (i === 0 ? 1 : 0); d < digits[i]; d++) {
if (!used.has(d)) {
let count = 1;
for (let j = i + 1; j < len; j++) {
count *= (10 - used.size - (j - i));
}
total += count;
}
}
if (used.has(digits[i]) || digits[i] === 0 && i === 0) break;
used.add(digits[i]);
}
if (used.size === len) total++;
return total;
};