Partition Labels(CPP easy to understand)
60

class Solution {
public:
vector partitionLabels(string s) {
vectorend_idx(26,0);
for(int i=0;i<s.length();i++){
end_idx[s[i]-'a']=i;
}
vectorresult;
int start=0,end=0;
for(int i=0;i<s.length();i++){
end=max(end,end_idx[s[i]-'a']);
if(i==end){//all the characters of current partition included
result.push_back(i-start+1);
start=i+1;
}
}
return result;
}
};

Comments (0)