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
|
#include <iostream>
#include <cstring>
class Solution {
public:
int maximumLength(std::string s) {
int n = s.length();
int cnt = 0;
int cnts[26][3] = { 0 };
for (int i = 0; i < n; ++i) {
++cnt;
if (i + 1 == n || s[i] != s[i + 1]) {
int idx = s[i] - 'a';
for (int j = 0; j < 3; ++j) {
if (cnts[idx][j] < cnt) {
for (int k = 2; k > j; --k) {
cnts[idx][k] = cnts[idx][k - 1];
}
cnts[idx][j] = cnt--;
}
}
cnt = 0;
}
}
int res = 0;
for (int i = 0; i < 26; ++i) {
res = std::max(res, cnts[i][2]);
}
return res == 0 ? -1 : res;
}
};
|