-
Notifications
You must be signed in to change notification settings - Fork 154
/
count-numbers-with-unique-digits.js
58 lines (50 loc) · 1.12 KB
/
count-numbers-with-unique-digits.js
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
48
49
50
51
52
53
54
55
56
57
58
/**
* Count Numbers with Unique Digits
*
* Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
*
* Example:
* Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100,
* excluding [11,22,33,44,55,66,77,88,99])
*/
/**
* @param {number} n
* @return {number}
*/
const countNumbersWithUniqueDigitsR = n => {
const result = [];
backtracking(n, 0, 0, new Set(), result);
return result.length;
};
const backtracking = (n, start, num, set, result) => {
if (start === n) {
result.push(num);
return;
}
for (let i = 0; i <= 9; i++) {
if (!set.has(i) || num === 0) {
set.add(i);
backtracking(n, start + 1, num * 10 + i, set, result);
set.delete(i);
}
}
};
/**
* @param {number} n
* @return {number}
*/
const countNumbersWithUniqueDigits = n => {
if (n === 0) {
return 1;
}
let count = 10;
let unique = 9;
let ok = 9;
while (n-- > 1 && ok > 0) {
unique = unique * ok;
count += unique;
ok--;
}
return count;
};
export { countNumbersWithUniqueDigitsR, countNumbersWithUniqueDigits };