1208.cpp (454B)
1 class Solution { 2 public: 3 int equalSubstring(const string &s, const string &t, const int maxCost) const { 4 const int n = size(s); 5 int res = 0, total = 0; 6 7 for (int i = 0, j = 0; i < n; i++) { 8 total += abs(s[i] - t[i]); 9 while (maxCost < total) { 10 total -= abs(s[j] - t[j]); 11 j++; 12 } 13 res = max(res, i - j + 1); 14 } 15 16 return res; 17 } 18 };