-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path763.cpp
More file actions
32 lines (32 loc) · 924 Bytes
/
763.cpp
File metadata and controls
32 lines (32 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
bool isValid(vector<bool>& used, vector<int>& counts) {
for (int i = 0; i < 26; ++i) {
if (used[i]) {
if (counts[i] != 0) return false;
}
}
return true;
}
vector<int> partitionLabels(string s) {
vector<int> counts(26, 0);
for (auto& c : s) counts[c - 'a']++;
vector<int> res;
int index = 0;
int n = s.size();
while (index < n) {
int start = index;
vector<bool> used(26, false);
used[s[index] - 'a'] = true;
counts[s[index] - 'a']--;
while (!isValid(used, counts) && index + 1 < n) {
index++;
used[s[index] - 'a'] = true;
counts[s[index] - 'a']--;
}
res.push_back(index - start + 1);
index++;
}
return res;
}
};