Skip to content

Commit fd3b400

Browse files
authored
Create 2981.cpp
1 parent ae905e1 commit fd3b400

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2981.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <algorithm>
2+
#include <string>
3+
#include <string_view>
4+
#include <unordered_map>
5+
6+
class Solution {
7+
bool validString(std::string_view s, int n) {
8+
std::unordered_map<std::string_view, int> um;
9+
for (size_t i = 0; i + n <= s.size(); ++i) {
10+
auto temp_view = s.substr(i, n);
11+
if (std::all_of(temp_view.begin(), temp_view.end(),
12+
[&](char c) { return c == temp_view.front(); })) {
13+
um[temp_view]++;
14+
}
15+
}
16+
for (const auto& p : um) {
17+
if (p.second >= 3) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
public:
25+
int maximumLength(std::string s) {
26+
int left = 1, right = s.size();
27+
int result = -1;
28+
while (left <= right) {
29+
int mid = left + (right - left) / 2;
30+
if (validString(s, mid)) {
31+
result = mid;
32+
left = mid + 1;
33+
} else {
34+
right = mid - 1;
35+
}
36+
}
37+
return result;
38+
}
39+
};

0 commit comments

Comments
 (0)