Description
Solution
双指针
Code
class Solution {
public:
inline int MAX(int a,int b) {return a > b ? a : b;}
int get_mx(int *sum) {
int mx = 0;
for(int i = 0;i < 26;++i) mx = MAX(mx, sum[i]);
return mx;
}
int characterReplacement(string s, int k) {
int sum[27], n = s.size(), res = k;
if(n == 0) return 0;
int l = 0, r = -1, len = 0, mx = 0;
for(int i = 0;i < 26;++i) sum[i] = 0;
while(l < n) {
int ch, mx;
while(r + 1 < n) {
ch = s[r+1] - 'A';
sum[ch]++, len++;
mx = get_mx(sum);
if(len - mx > k) {
len--, sum[ch]--;break;
}r++;
}res = MAX(res, len);
ch = s[l]-'A',sum[ch]--,len--,l++;
}
return res;
}
};