-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
34 lines (28 loc) · 913 Bytes
/
Solution.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
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int characterReplacement(string s, int k) {
if (s.length() < 2) {
return s.length();
}
int result = 0;
vector<int> count = vector<int>(26, 0);
int maxCount = 0;
int l = 0;
for (int r = 0; r < s.length(); ++r) {
maxCount = max(maxCount, ++count[s[r] - 'A']);
// If all k replacements have been used up, adjust the window
// `r - l + 1 - maxCount` indicates the number of replacements used
// where `r - l + 1` indicate the length of the window.
while (r - l + 1 - maxCount > k) {
--count[s[l++] - 'A']; // some prefix/postfix addition fun here
// Not necessary to find search for the maximum count.
// maxCount = *max_element(count.begin(), count.end());
}
result = max(result, r - l + 1);
}
return result;
}
};