class Solution {
public int equalSubstring(String s, String t, int maxCost) {
int n = s.length();
int[] cost = new int[n];
for (int i = 0; i < n; i++) {
cost[i] = Math.abs(s.charAt(i) - t.charAt(i));
}
int maxLength = 0;
int currentCost = 0;
int windowStart = 0;
for (int windowEnd = 0; windowEnd < n; windowEnd++) {
currentCost += cost[windowEnd];
while (currentCost > maxCost) {
currentCost -= cost[windowStart];
windowStart++;
}
maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
}
return maxLength;
}
}
𝗢𝗽𝗲𝗻 𝘁𝗼 𝗨𝗽𝗱𝗮𝘁𝗲𝘀 𝗮𝗻𝗱 𝗦𝘂𝗴𝗴𝗲𝘀𝘁𝗶𝗼𝗻𝘀
Top comments (0)