-
Notifications
You must be signed in to change notification settings - Fork 3
/
395.cpp
39 lines (35 loc) · 1.13 KB
/
395.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
int longestSubstring(string s, int k) {
int res = 0;
int n = s.size();
// allow count in s
for (int count = 1; count <= 26; ++count) {
int left = 0;
int right = 0;
vector<int> counter(26, 0);
int unique = 0;
while (right < n) {
bool flag = true;
int index = s[right] - 'a';
if (counter[index] == 0) unique++;
counter[index]++;
while (unique > count) {
int indexLeft = s[left] - 'a';
if (counter[indexLeft] == 1) unique--;
counter[indexLeft]--;
left++;
}
for (int i = 0; i < 26; ++i) {
if (counter[i] > 0 && counter[i] < k) {
flag = false;
break;
}
}
if (flag) res = max(res, right - left + 1);
right++;
}
}
return res;
}
};