✅97.44%🔥Easy solution🔥With explanation🔥
Intuition\n#### The problem requires finding the longest substring where the cost of converting it from s to t does not exceed maxCost. To achieve this, we can 
# Intuition\n#### The problem requires finding the longest substring where the cost of converting it from `s` to `t` does not exceed `maxCost`. To achieve this, we can utilize a sliding window approach to dynamically adjust the size of the substring while ensuring the cost constraint is respected.\n\n----\n\n# Approach\n### 1. Initialize Variables:\n - ##### `start` to mark the beginning of the sliding window.\n - ##### `current_cost` to keep track of the total transformation cost of the current window.\n - ##### `max_length` to store the maximum length of a valid window found.\n### 2. Iterate through the String:\n - ##### Use `end` as the current end of the window.\n - ##### For each character at `end`, compute the cost to convert `s[end]` to `t[end]` and add it to `current_cost`.\n\n### 3. Adjust the Window:\n\n - ##### If `current_cost` exceeds `maxCost`, increment `start` to shrink the window from the left, subtracting the cost of the character at `start` from `current_cost`, until the total cost is within the allowed limit.\n\n### 4. Update the Maximum Length:\n\n - ##### After adjusting the window, compare its size `(end - start + 1)` with `max_length` and update `max_length` if the current window is larger.\n\n### 5. Return the Result:\n- ##### The maximum valid window length found during the iteration is returned as the result.\n\n----\n\n# Complexity\n- ### Time complexity:\n#### $$O(n)$$, where n is the length of the string `s` (or `t`). This is because each character is processed at most twice (once when expanding the window and once when shrinking it).\n\n- ### Space complexity:\n#### $$O(1)$$, as we are using a constant amount of extra space regardless of the input size.\n---\n\n# Code\n```python []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        n = len(s)\n        start = 0\n        current_cost = 0\n        max_length = 0\n\n        for end in range(n):\n            current_cost += abs(ord(s[end]) - ord(t[end]))\n\n            while current_cost > maxCost:\n                current_cost -= abs(ord(s[start]) - ord(t[start]))\n                start += 1\n\n            max_length = max(max_length, end - start + 1)\n        \n        return max_length\n```\n```C++ []\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int n = s.size();\n        int start = 0;\n        int current_cost = 0;\n        int max_length = 0;\n\n        for (int end = 0; end < n; ++end) {\n            current_cost += abs(s[end] - t[end]);\n\n            while (current_cost > maxCost) {\n                current_cost -= abs(s[start] - t[start]);\n                ++start;\n            }\n\n            max_length = max(max_length, end - start + 1);\n        }\n\n        return max_length;\n    }\n};\n\n```\n```java []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int n = s.length();\n        int start = 0;\n        int currentCost = 0;\n        int maxLength = 0;\n\n        for (int end = 0; end < n; ++end) {\n            currentCost += Math.abs(s.charAt(end) - t.charAt(end));\n\n            while (currentCost > maxCost) {\n                currentCost -= Math.abs(s.charAt(start) - t.charAt(start));\n                ++start;\n            }\n\n            maxLength = Math.max(maxLength, end - start + 1);\n        }\n\n        return maxLength;\n    }\n}\n\n```\n```javascript []\nvar equalSubstring = function(s, t, maxCost) {\n    let n = s.length;\n    let start = 0;\n    let currentCost = 0;\n    let maxLength = 0;\n\n    for (let end = 0; end < n; end++) {\n        currentCost += Math.abs(s.charCodeAt(end) - t.charCodeAt(end));\n\n        while (currentCost > maxCost) {\n            currentCost -= Math.abs(s.charCodeAt(start) - t.charCodeAt(start));\n            start++;\n        }\n\n        maxLength = Math.max(maxLength, end - start + 1);\n    }\n\n    return maxLength;\n};\n```\n```C# []\npublic class Solution {\n    public int EqualSubstring(string s, string t, int maxCost) {\n        int n = s.Length;\n        int start = 0;\n        int currentCost = 0;\n        int maxLength = 0;\n\n        for (int end = 0; end < n; ++end) {\n            currentCost += Math.Abs(s[end] - t[end]);\n\n            while (currentCost > maxCost) {\n                currentCost -= Math.Abs(s[start] - t[start]);\n                ++start;\n            }\n\n            maxLength = Math.Max(maxLength, end - start + 1);\n        }\n\n        return maxLength;\n    }\n}\n\n```\n---\n\n![Screenshot 2023-08-20 065922.png](https://assets.leetcode.com/users/images/9155e345-3b35-4caa-9705-8bb578c6acbf_1716855932.019017.png)\n\n
['String', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']