0424.cpp (617B)
1 class Solution { 2 public: 3 int characterReplacement(string s, int k) { 4 vector<int> counts(26, 0); 5 int n = s.length(), start = 0, maxi = 0, result = 0; 6 for_each(s.begin(), s.end(), [](char &c) { c -= 'A'; }); 7 8 for (int end = 0; end < n; end++) { 9 maxi = max(maxi, ++counts[s[end]]); 10 while (end - start - maxi + 1 > k) { 11 counts[s[start++]]--; 12 for (int i = 0; i < 26; i++) 13 maxi = max(maxi, counts[i]); 14 } 15 result = max(result, end - start + 1); 16 } 17 return result; 18 } 19 };