DEV Community

Karleb
Karleb

Posted on

#1208. Get Equal Substrings Within Budget

https://leetcode.com/problems/get-equal-substrings-within-budget/description/?envType=daily-question&envId=2024-05-28


/**
 * @param {string} s
 * @param {string} t
 * @param {number} maxCost
 * @return {number}
 */
var equalSubstring = function(s, t, maxCost) {
    let n = s.length, start = 0, currCost = 0, maxLen = 0

    for (let end = 0; end < n; end++) {
        currCost += Math.abs(s.charCodeAt(end) - t.charCodeAt(end))

        while(currCost > maxCost) {
            currCost -= Math.abs(s.charCodeAt(start) - t.charCodeAt(start))
            start++
        }

        maxLen = Math.max(maxLen, end - start + 1)
    }

    return maxLen
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)