-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode_2516.cpp
63 lines (56 loc) · 1.42 KB
/
LeetCode_2516.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int takeCharacters(string s, int k) {
// Total counts
int n = s.size();
int count_a = 0;
int count_b = 0;
int count_c = 0;
for (const auto& it : s) {
if (it == 'a') {
count_a++;
} else if (it == 'b') {
count_b++;
} else {
count_c++;
}
}
if (count_a < k || count_b < k || count_c < k) {
return -1;
}
int i = 0;
int j = 0;
int notDeletedWindow = 0;
while (j < n) {
char ch = s[j];
if (ch == 'a') {
count_a--;
} else if (ch == 'b') {
count_b--;
} else {
count_c--;
}
while (i <= j && (count_a < k || count_b < k || count_c < k)) {
char ch = s[i];
if (ch == 'a') {
count_a++;
} else if (ch == 'b') {
count_b++;
} else {
count_c++;
}
i++;
}
notDeletedWindow = max(notDeletedWindow, j - i + 1);
j++;
}
return n - notDeletedWindow;
}
};
int main() {
Solution obj;
cout << obj.takeCharacters("abacba", 2) << endl;
return 0;
}