classSolution{public:vector<int>partitionLabels(stringS){vector<int>ans;vector<int>rightmost(128);for(inti=0;i<S.length();++i)rightmost[S[i]]=i;intl=0;// First index of current running stringintr=0;// Right most so farfor(inti=0;i<S.length();++i){r=max(r,rightmost[S[i]]);if(r==i){ans.push_back(i-l+1);l=i+1;}}returnans;}};
classSolution{publicList<Integer>partitionLabels(StringS){List<Integer>ans=newArrayList<>();int[]rightmost=newint[128];for(inti=0;i<S.length();++i)rightmost[S.charAt(i)]=i;intl=0;// First index of current running stringintr=0;// Right most so farfor(inti=0;i<S.length();++i){r=Math.max(r,rightmost[S.charAt(i)]);if(r==i){ans.add(i-l+1);l=i+1;}}returnans;}}