Skip to content

Commit

Permalink
add leetcode q2982
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoHN committed Jun 11, 2024
1 parent 5f5fc7c commit 2dad744
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions trivial/leetcode/source/q2982.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "common.h"

class Solution {
public:
int maximumLength(string s) {
// 只需记录超过 3 的最长的字符串
vector<vector<int>> len(26, vector<int>());
for (int i = 0; i < s.size(); i++) {
for (int j = i; i + j < s.size() && s[i] == s[i + j]; j++) {
len[s[i] - 'a'].emplace_back(j - i + 1);
}
}

int max_cnt = -1;

for (int i = 0; i < 26; i++) {
sort(len[i].begin(), len[i].end(), greater<int>());
for (int j = 0; j + 2 < len[i].size(); ++j) {
if (len[i][j] == len[i][j + 1] && len[i][j + 1] == len[i][j + 2]) {
max_cnt = max(max_cnt, len[i][j]);
}
}
}
return max_cnt;
}
};

int main(int argc, char const* argv[]) {
Solution so;
print(so.maximumLength("aaaa"));
return 0;
}

0 comments on commit 2dad744

Please sign in to comment.