Skip to content

Commit 725d1b1

Browse files
authored
Update 357-count-numbers-with-unique-digits.js
1 parent 329147b commit 725d1b1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

357-count-numbers-with-unique-digits.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,27 @@ const countNumbersWithUniqueDigits = function(n) {
1616

1717
return res;
1818
};
19+
20+
21+
// another
22+
23+
/**
24+
* @param {number} n
25+
* @return {number}
26+
*/
27+
const countNumbersWithUniqueDigits = function(n) {
28+
const limit = 10 ** n
29+
let res = 1
30+
let m = 1
31+
if(n === 0) return 1
32+
while(10**m <= limit) {
33+
res += 9 * helper(9, m - 1)
34+
m++
35+
}
36+
37+
return res
38+
39+
function helper(m, n) {
40+
return n === 0 ? 1 : helper(m, n - 1) * (m - n + 1)
41+
}
42+
};

0 commit comments

Comments
 (0)