-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 106.java
More file actions
29 lines (23 loc) · 732 Bytes
/
Day 106.java
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 {
public int countSubstr(String s, int k) {
return atMostK(s, k) - atMostK(s, k - 1);
}
private int atMostK(String s, int k) {
if (k < 0) return 0;
int[] freq = new int[26];
int left = 0, distinct = 0, count = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (freq[c - 'a'] == 0) distinct++;
freq[c - 'a']++;
while (distinct > k) {
char l = s.charAt(left);
freq[l - 'a']--;
if (freq[l - 'a'] == 0) distinct--;
left++;
}
count += (right - left + 1);
}
return count;
}
}