File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments