🔥🔥 Diagrammatic  Explanation 👁️👁️
What the question is saying, we have two strings and have maxCost. Keep maxCost in your mind and tell, a big to big s string can be converted to t string. And i
What the question is saying, we have **two strings** and have **maxCost**. Keep **maxCost** in your mind and tell, a big to big **s** string can be converted to **t** string. And if we converted the whole string, how much will be the cost!\n\nLet\\s undrestand with an example;\n```\nInput: s = "abcd", t = "bcdf", maxCost = 3\nOutput: 3\n```\n![image](https://assets.leetcode.com/users/images/70f5b1cc-775d-42f0-802a-ffc955a3107f_1716855070.8225849.png)\n\n> But, what the question is saying! We have to be in the **maxCost**\n> > What it saying is, get that string out! Which can be converted within the given **maxCost**\n\nSo, in the above example! **abc** length is **three** and it can be converted into **bcd** within this **maxCost**\n\n> But what if were unable to conert, then what will be! Let;s take one more example!\n\n```\nInput: s = "abcd", t = "acde", maxCost = 0\nOutput: 1\n```\n![image](https://assets.leetcode.com/users/images/d60ce5ae-0677-4324-bcd6-82de398a8636_1716855412.906484.png)\nNow, as **a == a** and how we got the **Output 1** because, we just have to get the length out from the string! How long a string can be converted and return it\'s length!\n\nSo, how we going to code is,\n\n1. Initialize `n` to the length of string `s`.\n2. Initialize `ans` to 0, which will hold the maximum length of the substring that can be changed within the given cost.\n3. Initialize `window` to 0, which will accumulate the cost of changing the substring from `s` to `t`.\n4. Initialize `left` to 0, which will be the starting index of the sliding window.\n5. Loop through the string using `right` as the ending index of the sliding window.\n6. Add the cost of changing `s[right]` to `t[right]` to `window`.\n7. If `window` exceeds `maxCost`, shrink the window from the left by subtracting the cost of changing `s[left]` to `t[left]` and incrementing `left`.\n8. Update `ans` with the maximum length of the substring found so far.\n9. Return the maximum length `ans`.\n\n```plaintext\nFunction equalSubstring(s, t, maxCost):\n    n = length of s\n    ans = 0\n    window = 0\n    left = 0\n\n    For right from 0 to n - 1:\n        window += absolute difference between ASCII values of s[right] and t[right]\n\n        While window > maxCost:\n            window -= absolute difference between ASCII values of s[left] and t[left]\n            left += 1\n\n        ans = maximum of ans and (right - left + 1)\n\n    Return ans\n```\n\n**Let\'s Code it UP** *ladies n gentlemen*\n\n**C++**\n```\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int n = s.length();\n        int ans = 0, window = 0, left = 0;\n        for (int right = 0; right < n; right++) {\n            window += abs(s[right] - t[right]);\n            while (window > maxCost) {\n                window -= abs(s[left] - t[left]);\n                left++;\n            }\n            ans = max(ans, right - left + 1);\n        }\n        return ans;\n    }\n};\n```\n**JAVA**\n```\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int n = s.length();\n        int ans = 0, window = 0, left = 0;\n        for (int right = 0; right < n; right++) {\n            window += Math.abs(s.charAt(right) - t.charAt(right));\n            while (window > maxCost) {\n                window -= Math.abs(s.charAt(left) - t.charAt(left));\n                left++;\n            }\n            ans = Math.max(ans, right - left + 1);\n        }\n        return ans;\n    }\n}\n```\n**PYTHON**\n```\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        n = len(s)\n        ans = 0\n        window = 0\n        left = 0\n        \n        for right in range(n):\n            window += abs(ord(s[right]) - ord(t[right]))\n            while window > maxCost:\n                window -= abs(ord(s[left]) - ord(t[left]))\n                left += 1\n            ans = max(ans, right - left + 1)\n        \n        return ans\n```\n\n___\nComplexity Analysis\n\n___\n* **Time Complexity :-** BigO(N) \n* **Space Complexity :-** BigO(1)
['C', 'Python', 'Java']