-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2981.cpp
More file actions
29 lines (28 loc) · 951 Bytes
/
2981.cpp
File metadata and controls
29 lines (28 loc) · 951 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
class Solution {
public:
int maximumLength(string s) {
vector<vector<int>> lens(26);
int n = s.size();
int index = 0;
while (index < n) {
char c = s[index];
int start = index;
while (index + 1 < n && s[index + 1] == c) index++;
lens[c - 'a'].push_back(index - start + 1);
index++;
}
int res = -1;
for (auto& len : lens) {
sort(len.begin(), len.end());
if (len.size() == 0) continue;
int m = len.size();
for (int j = 0; j < m; ++j) {
if (len[j] >= 3) res = max(res, len[j] - 2);
if (j + 2 < m) res = max(res, len[j]);
if (j + 1 < m && len[j + 1] >= len[j] + 1) res = max(res, len[j]);
if (j + 1 < m && len[j + 1] == len[j] && len[j] > 1) res = max(res, len[j] - 1);
}
}
return res;
}
};