class Solution {
public List<Integer> partitionLabels(String S) {
int[] lastIndexOfChar=new int[26];
for(int i=0;i<S.length();i++) {
lastIndexOfChar[S.charAt(i)-'a']=i;
}
List<Integer> partitions=new ArrayList<Integer>();
int firstIndex=0;
while(firstIndex<S.length()) {
int lastIndex=firstIndex;
for(int i=firstIndex;i<S.length() && i<=lastIndex;i++) {
int index=lastIndexOfChar[S.charAt(i)-'a'];
if(index>lastIndex) {
lastIndex=index;
}
}
partitions.add(lastIndex-firstIndex+1);
firstIndex=lastIndex+1;
}
return partitions;
}
}
难!!!