Python | Sliding Window
see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        start = 0\n  
[see the Successfully Accepted Submission](https://leetcode.com/problems/get-equal-substrings-within-budget/submissions/1270083696/?source=submission-ac)\n\n# Code\n```\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        start = 0\n        currentCost = 0\n        maxLength = 0\n        \n        for end in range(len(s)):\n            currentCost += abs(ord(s[end]) - ord(t[end]))\n            \n            while currentCost > maxCost:\n                currentCost -= abs(ord(s[start]) - ord(t[start]))\n                start += 1\n            \n            maxLength = max(maxLength, end - start + 1)\n        \n        return maxLength\n\n```\n\n### Initialize Variables\n\n- `start` to keep track of the start of the current window.\n- `currentCost` to keep track of the cost of transforming the current window substring.\n- `maxLength` to store the maximum length of a valid substring.\n\n### Sliding Window\n\n1. Iterate through each character of the strings `s` and `t` using an index `end`.\n2. For each character, calculate the transformation cost `|s[end] - t[end]|` and add it to `currentCost`.\n3. If `currentCost` exceeds `maxCost`, shrink the window from the left by incrementing `start` until `currentCost` is less than or equal to `maxCost`.\n4. Update `maxLength` with the size of the current window (`end - start + 1`) if it is greater than the previously recorded `maxLength`.\n\n### Edge Cases\n\n- If `maxCost` is zero, the algorithm should still handle it correctly, as any cost greater than zero will automatically shrink the window to ensure the current cost is zero or less.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']