/*
1. 取第一个字符,记住它的start位置,然后反向查找这个字符,记住它的位置end
2. 把这个字符加入到set中。
3. 然后从start+1开始到end,如果start+1字符不在set中,反向查找这个字符,如果找到的位置大于end,更新新的end。
4. 循环结束后得到一个分割。然后把剩下的字符串【end+1,S.end()),转1.
*/
class Solution {
public:vector<int> partitionLabels(string S) {
vector<int> ret;
help(ret,S);
return ret;
}
private:
void help(vector<int>& ret, string S)
{
if(S.size() == 0) return;
int start = 0;
char ch = S[start];
int end = S.rfind(ch);
int pos = 0;
unordered_set<char> mySet;
mySet.insert(ch);
while( ++start<=end )
{
if(!mySet.count(S[start]))
{
pos = S.rfind(S[start]);
mySet.insert(S[start]);
end = max(end,pos);
}
}
ret.push_back(end+1);
help(ret,S.substr(end+1));
}
};