Sliding window
Approach\n Describe your approach to solving the problem. \nUse the classic sliding window approach to solve this problem with two pointers (left pointer and ri
# Approach\n<!-- Describe your approach to solving the problem. -->\nUse the classic sliding window approach to solve this problem with two pointers (left pointer and right pointer) to find the longest valid substring. \n\nIterate through the differences in ASCII values between corresponding characters of s and t with the right pointer to expand the window by adding the cost of the current character to currentCost. If current cost exceeds max cost, contract the window by moving the left pointer to the right and subtracting the cost of the character at the left pointer from current cost until current cost is within maxCost. Update maxLength with the length of the current valid window.\n\n# Complexity\n- Time complexity: $O(N)$ where $N$ is string length, because rightPointer increment each steps. \n\n- Space complexity: $O(1)$ additional memory\n\n# Code\n```\nclass Solution {\n    fun equalSubstring(s: String, t: String, maxCost: Int): Int {\n        var currentCost = 0\n        var maxLength = 0\n        var leftPointer = 0\n        \n        for (rightPointer in s.indices) {\n            currentCost += abs(s[rightPointer] - t[rightPointer])\n            while (leftPointer <= rightPointer && currentCost > maxCost) {\n                currentCost -= abs(s[leftPointer] - t[leftPointer])\n                leftPointer++\n            }\n            maxLength = max(maxLength, rightPointer - leftPointer + 1)\n        }\n        \n        return maxLength\n    }\n}\n```\n\nMost of my solutions are in [C++](https://github.com/drgavrikov/leetcode-cpp]) and [Kotlin](https://github.com/drgavrikov/leetcode-jvm) on my [Github](https://github.com/drgavrikov).
['Sliding Window', 'Kotlin']