滑动窗口,右边无脑滑动即可,左边看情况收缩
class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int len = s.length(), sum = 0, result = 0;
for(int left = 0, right = 0; right < len; right++){
sum += Math.abs(s.charAt(right) - t.charAt(right));
if(sum > maxCost){
sum -= Math.abs(s.charAt(left) - t.charAt(left));
++left;
}
result = Math.max(result, right - left + 1);
}
return result;
}
}