Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created March 8, 2018 15:04
Show Gist options
  • Save s4553711/af72b833f0c2d7df28e54f3e3db88da0 to your computer and use it in GitHub Desktop.
Save s4553711/af72b833f0c2d7df28e54f3e3db88da0 to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<int> partitionLabels(string S) {
vector<int> last_indx(128, 0);
for(int i = 0; i < S.size(); i++)
last_indx[S[i]] = i;
vector<int> ans;
int start = 0;
int end = 0;
for(int i = 0; i < S.size(); i++) {
// cout << "1> i: " << i << " ,end: " << end << endl;
end = max(end, last_indx[S[i]]);
// cout << "2> i: " << i << " ,end: " << end << endl;
if (i == end) {
ans.push_back(end - start + 1);
start = end + 1;
}
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment