C++


  • 0
    N
    class Solution {
    public:
        vector<int> partitionLabels(string S) {
            unordered_map<char,int> map;
            for(int i=0; i<S.size(); ++i)
                map[S[i]] = i;
            
            vector<int> ans;
            int begin = 0;
            int end = 0;
            for(int i=0; i<S.size(); ++i){
                if(map[S[i]] > end)
                    end = map[S[i]];
                if(i == end){
                    ans.push_back(end-begin+1);
                    begin = end+1;
                }
            }
            
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.