-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (23 loc) · 774 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (23 loc) · 774 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
30
class Solution {
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++) {
if (freq[s.charAt(right) - 'a'] == 0)
distinct++;
freq[s.charAt(right) - 'a']++;
while (distinct > k) {
freq[s.charAt(left) - 'a']--;
if (freq[s.charAt(left) - 'a'] == 0)
distinct--;
left++;
}
count += (right - left + 1);
}
return count;
}
public int countSubstr(String s, int k) {
return atMostK(s, k) - atMostK(s, k - 1);
}
}