We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f8a0930 commit dc07b82Copy full SHA for dc07b82
2376-count-special-integers.js
@@ -0,0 +1,27 @@
1
+/**
2
+ * @param {number} n
3
+ * @return {number}
4
+ */
5
+var countSpecialNumbers = 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
+ limit = L.length;
12
+ for (let i = 1; i < limit; ++i) res += 9 * A(9, i - 1);
13
14
+ const seen = new Set();
15
+ for (let i = 0; i < limit; ++i) {
16
+ for (let j = i > 0 ? 0 : 1; j < L[i]; ++j)
17
+ if (!seen.has(j)) res += A(9 - i, limit - i - 1);
18
+ if (seen.has(L[i])) break;
19
+ seen.add(L[i]);
20
+ }
21
+ return res;
22
+};
23
24
25
+function A(m, n) {
26
+ return n === 0 ? 1 : A(m, n - 1) * (m - n + 1);
27
+}
0 commit comments