-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path1100.java
24 lines (22 loc) · 1.02 KB
/
1100.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
__________________________________________________________________________________________________
class Solution {
public int numKLenSubstrNoRepeats(String S, int K) {
if (S.length() < K) return 0;
int res = 0;
Map<Character, Integer> map = new HashMap<>();
int i = 0;
int j = 0;
for (; j < K - 1; j++) {
map.put(S.charAt(j), map.getOrDefault(S.charAt(j), 0) + 1);
}
for (; j < S.length(); j++,i++) {
map.put(S.charAt(j), map.getOrDefault(S.charAt(j), 0) + 1);
if (map.size() == K) res++;
map.put(S.charAt(i), map.getOrDefault(S.charAt(i), 0) - 1);
if (map.get(S.charAt(i)) == 0) map.remove(S.charAt(i));
}
return res;
}
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________