𝗦𝘁𝗿𝗲𝗮𝗸- 𝟮 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲- 𝗚𝗲𝘁 𝗘𝗾𝘂𝗮𝗹 𝗦𝘂𝗯𝘀𝘁𝗿𝗶𝗻𝗴𝘀 𝗪𝗶𝘁𝗵𝗶𝗻 𝗕𝘂𝗱𝗴𝗲𝘁

Pranjal Sailwal - May 28 - - Dev Community
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;
    }
}
Enter fullscreen mode Exit fullscreen mode

𝗢𝗽𝗲𝗻 𝘁𝗼 𝗨𝗽𝗱𝗮𝘁𝗲𝘀 𝗮𝗻𝗱 𝗦𝘂𝗴𝗴𝗲𝘀𝘁𝗶𝗼𝗻𝘀

. . . . . . . . . .