-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lc0395Solution.java
33 lines (31 loc) · 998 Bytes
/
Lc0395Solution.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
25
26
27
28
29
30
31
32
33
package sliding_window;
import java.util.HashMap;
import java.util.Map;
public class Lc0395Solution {
public int longestSubstring(String s, int k) {
// 这也算滑动窗口法吗?
// 感觉思路不难,自己却有思维定式,菜!
if (s == null || s.length() == 0) {
return 0;
}
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < s.length(); i++) {
if (map.get(s.charAt(i)) < k) {
sb.setCharAt(i, ',');
}
}
String[] strings = sb.toString().split(",");
if (strings.length == 1) {
return strings[0].length();
}
int max = 0;
for (String string : strings) {
max = Math.max(max, longestSubstring(string, k));
}
return max;
}
}