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)