A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.

Example 1:
Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
 
Note:
S will have length in range [1, 500].
S will consist of lowercase English letters ('a' to 'z') only.

运行结果:

class Solution {
public:
    vector<int> partitionLabels(string S) {
        int last_pos[26] = {0};
        int elem_cnt = S.size();
        //记录字母最后出现位置
        for(int i = 0;i < elem_cnt;i++)
            last_pos[S[i]-'a'] = i; 
        
        vector<int> res;
        int start = 0,last = 0;
        for(int i = 0;i < elem_cnt;i++)
        {
            last = max(last,last_pos[S[i] -'a']);
            if(i == last)
            {
                res.push_back(last - start + 1);
                start = last + 1;
            }
        }
       
        return res;
    }
};
Runtime: 4 ms, faster than 97.63% of C++ online submissions for Partition Labels.
Memory Usage: 6.9 MB, less than 34.09% of C++ online submissions for Partition Labels.