-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.js
More file actions
29 lines (23 loc) · 732 Bytes
/
Copy pathsolution.js
File metadata and controls
29 lines (23 loc) · 732 Bytes
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
class Solution {
atMostK(s, k) {
if (k < 0) return 0;
const freq = new Array(26).fill(0);
let left = 0, distinct = 0, count = 0;
for (let right = 0; right < s.length; right++) {
const r = s.charCodeAt(right) - 97;
if (freq[r] === 0) distinct++;
freq[r]++;
while (distinct > k) {
const l = s.charCodeAt(left) - 97;
freq[l]--;
if (freq[l] === 0) distinct--;
left++;
}
count += (right - left + 1);
}
return count;
}
countSubstr(s, k) {
return this.atMostK(s, k) - this.atMostK(s, k - 1);
}
}