leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0763.cpp (670B)


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