-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3713.cpp
More file actions
26 lines (26 loc) · 706 Bytes
/
3713.cpp
File metadata and controls
26 lines (26 loc) · 706 Bytes
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
class Solution {
public:
bool valid(int* hash) {
int x = 0;
for (int i = 0; i < 26; ++i) {
if (hash[i] != 0) {
if (x == 0) x = hash[i];
else if (hash[i] != x) return false;
}
}
return true;
}
int longestBalanced(string s) {
int hash[26];
int n = s.size();
int res = 0;
for (int start = 0; start < n; ++start) {
for (int i = 0; i < 26; ++i) hash[i] = 0;
for (int end = start; end < n; ++end) {
hash[s[end] - 'a']++;
if (valid(hash)) res = max(res, end - start + 1);
}
}
return res;
}
};