-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
42 lines (42 loc) · 1.23 KB
/
Solution.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
34
35
36
37
38
39
40
41
42
public class Solution {
public int longestSubstring(String s, int k) {
char[] cs=s.toCharArray();
Map<Character, Integer> map=new HashMap<Character, Integer>();
for (Character c:cs) {
map.put(c, map.get(c)==null?1:map.get(c)+1);
}
Set<Character> cSet=new HashSet<>();
for (Character c:map.keySet()) {
if (map.get(c)<k) {
cSet.add(c);
}
}
if (cSet.size()==0) {
return s.length();
}
Set<String> set=new HashSet<String>();
Set<String> newSet=new HashSet<String>();
set.add(s);
for (Character c:cSet) {
for (String test:set) {
String[] strs=test.split(String.valueOf(c));
for (String str:strs) {
if (str.length()>0) {
newSet.add(str);
}
}
}
set.clear();
set.addAll(newSet);
newSet.clear();
}
int ans=0;
for (String test:set) {
int maxTest=longestSubstring(test,k);
if (maxTest>ans) {
ans=maxTest;
}
}
return ans;
}
}