leoanjos

Partition Labels

May 18th, 2022 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. class Solution {
  2. private:
  3.     const int MAX = 26;
  4. public:
  5.     vector<int> partitionLabels(string s) {
  6.         int last[MAX];
  7.         for (int i = 0; i < (int) s.size(); i++)
  8.             last[s[i] - 'a'] = i;
  9.        
  10.         int idx = 0;
  11.         vector<int> ans;
  12.        
  13.         while (idx < (int) s.size()) {
  14.             int size = 1, aux = idx;
  15.             while (aux < (int) s.size() && aux < idx + size) {
  16.                 size = max(size, last[s[aux++] - 'a'] - idx + 1);
  17.             }
  18.            
  19.             idx += size;
  20.             ans.push_back(size);
  21.         }
  22.        
  23.         return ans;
  24.     }
  25. };
Add Comment
Please, Sign In to add comment