0763.cpp (670B)
1 class Solution { 2 public: 3 vector<int> partitionLabels(string s) { 4 unordered_set<char> seen; 5 array<int, 26> count = {0}; 6 vector<int> res; 7 8 for (char c : s) 9 count[c - 'a']++; 10 11 int len = 0, needed = 0; 12 for (int i = 0; i < s.size(); i++) { 13 len++; 14 if (seen.count(s[i])) 15 needed--; 16 else { 17 needed += count[s[i] - 'a'] - 1; 18 seen.insert(s[i]); 19 } 20 21 if (!needed) { 22 res.push_back(len); 23 seen.clear(); 24 len = 0; 25 } 26 } 27 28 return res; 29 } 30 };