1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int equalSubstring(string s, string t, int maxCost) { const int n = s.size(); int left = 0, right = 0, cost = 0, ans = 0; while (right < n) { cost += abs(s[right] - t[right]); ++right; if (cost <= maxCost) { ans = max(ans, right - left); } else { while (cost > maxCost && left < right) { cost -= abs(s[left] - t[left]); ++left; } } } return ans; } };
|