-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2516.cpp
More file actions
36 lines (35 loc) · 1.02 KB
/
2516.cpp
File metadata and controls
36 lines (35 loc) · 1.02 KB
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
class Solution {
public:
int takeCharacters(string s, int k) {
int totalA = 0;
int totalB = 0;
int totalC = 0;
for (auto& c : s) {
if (c == 'a') totalA++;
else if (c == 'b') totalB++;
else totalC++;
}
if (totalA < k || totalB < k || totalC < k) return -1;
int n = s.size();
int left = 0;
int cntA = 0;
int cntB = 0;
int cntC = 0;
int window = 0;
for (int right = 0; right < n; ++right) {
if (s[right] == 'a') cntA++;
else if (s[right] == 'b') cntB++;
else cntC++;
while (cntA > totalA - k || cntB > totalB - k || cntC > totalC - k) {
if (s[left] == 'a') cntA--;
else if (s[left] == 'b') cntB--;
else cntC--;
left++;
}
if (left <= right) {
window = max(window, right - left + 1);
}
}
return n - window;
}
};