-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP424.cpp
58 lines (54 loc) · 1.58 KB
/
P424.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
#include "header.h"
#include <array>
#include <cstdlib>
#define N 10001
class Solution {
public:
int characterReplacement(string s, int k) {
int head, tail, n;
array<bool, 26> letterSet;
letterSet.fill(false);
n = s.size();
for (int i=0; i<n; i++)
letterSet[s[i]-'A'] = true;
int maxDist = 0;
for (int t=0; t<26; t++) {
if (letterSet[t] == false) continue;
head = tail = 0;
int currTransfer = 0;
int dist = 0;
while (tail < n) {
if (head>tail) {
tail=head;
currTransfer = 0;
}
if (currTransfer+1 <= k) {
if (s[tail]-'A' != t) currTransfer++;
tail++;
}
else {
if (s[tail]-'A' == t) {
tail++;
}
else {
if (s[head]-'A'!=t) currTransfer--;
head++;
}
}
if (tail-head > dist) dist = tail-head;
}
maxDist = max(maxDist, dist);
}
return maxDist;
}
};
int main(int argc, char const *argv[])
{
string s = "AAABBBABCAABVC";
int k = 4;
cout << Solution().characterReplacement(s,k) << endl;
cout << Solution().characterReplacement("ABAB",2) << endl;
cout << Solution().characterReplacement("AABABBA",1) << endl;
cout << Solution().characterReplacement("ABAA",0) << endl;
return 0;
}