贪心算法+双指针
class Solution {
public List<Integer> partitionLabels(String S) {
// 字符串S中每个字母最后出现的下标
int[] last = new int[26];
int length = S.length();
for (int i = 0; i < length; i++) {
last[S.charAt(i) - 'a'] = i;
}
// 遍历字符串
int start = 0, end = 0;
List<Integer> partitions = new ArrayList<>();
for (int i = 0; i < length; i++) {
end = Math.max(end,last[S.charAt(i) - 'a']);
if (i == end) {
partitions.add(end - start + 1);
start = end + 1;
}
}
return partitions;
}
}