leetcode-763. 划分字母区间

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
class Solution {
public List<Integer> partitionLabels(String S) {
List<Integer> result = new ArrayList<>();
int n = S.length();
int pre = 0;
Set<Character> set = new HashSet<>();
for (int i = 0; i < n; i++) {
set.add(S.charAt(i));
boolean b = judge(set, S.substring(i + 1));
if (!b) {
result.add(i - pre + 1);
pre = i + 1;
set.clear();
}
}
return result;
}

// 判断后面的字符串是否包含set中的内容
public boolean judge(Set<Character> set, String s) {
for (Character ch : set) {
if (s.contains(ch.toString())) {
return true;
}
}
return false;
}
}