class Solution { public int equalSubstring(String s, String t, int maxCost) { int left = 0, right =0; int sum = 0; int res = 0; // construct window while (right < s.length()) { sum += Math.abs(s.charAt(right) - t.charAt(right)); right++; // The window construction is completed, and the window size should be adjusted according to the current window while (sum > maxCost) { sum -= Math.abs(s.charAt(left) - t.charAt(left)); left++; } // Record the size of the window at this time res = Math.max(res, right -left); } return res; } }