Skip to content

Commit ad6f020

Browse files
authored
Create 1015-numbers-with-repeated-digits.js
1 parent 23f4c26 commit ad6f020

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

1015-numbers-with-repeated-digits.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} N
3+
* @return {number}
4+
*/
5+
const numDupDigitsAtMostN = function(N) {
6+
const L = [];
7+
for (let x = N + 1; x > 0; x = Math.floor(x / 10)) L.unshift(x % 10);
8+
9+
// Count the number with digits < N
10+
let res = 0,
11+
n = L.length;
12+
for (let i = 1; i < n; ++i) res += 9 * A(9, i - 1);
13+
14+
const seen = new Set();
15+
for (let i = 0; i < n; ++i) {
16+
for (let j = i > 0 ? 0 : 1; j < L[i]; ++j)
17+
if (!seen.has(j)) res += A(9 - i, n - i - 1);
18+
if (seen.has(L[i])) break;
19+
seen.add(L[i]);
20+
}
21+
return N - res;
22+
};
23+
24+
function A(m, n) {
25+
return n === 0 ? 1 : A(m, n - 1) * (m - n + 1);
26+
}

0 commit comments

Comments
 (0)