leetcode 2981.找出出现至少三次的最长子特殊字符串(传送门)
cpp
class Solution {
public:
int maximumLength(string s) {
int hash[30][52] = { 0 },len = 1,maxn=0;
char last = 'A';
for (char ch : s) {
if (ch == last) len++;
else len = 1;
for (int i = len; i > 0; i--) {
if(++hash[ch-'a'][i]>=3){
if(i>maxn)
maxn=i;
break;
}
}
last = ch;
}
return maxn?maxn:-1;
}
};