🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\nC++ []\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int l = 0, curr = 0, cnt = 0;\n        for 
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/7d8296eb-084f-4b80-b443-b6649c4e1378_1716868062.3889.jpeg)\n![Screenshot 2024-05-28 091715.png](https://assets.leetcode.com/users/images/9b5704ba-5b0f-4a39-a23e-a211344ee94b_1716868069.3606114.png)\n```C++ []\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int l = 0, curr = 0, cnt = 0;\n        for (int r = 0; s[r]; r++) {\n            curr += abs(s[r] - t[r]);\n            while (curr > maxCost) {\n                curr -= abs(s[l] - t[l]);\n                l++;\n            }\n            cnt = max(cnt, (r - l + 1));\n        }\n        return cnt;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        l, curr = 0, 0\n        res = 0\n        \n        for r in range(len(s)):\n            curr += abs(ord(s[r]) - ord(t[r]))\n            while curr > maxCost:\n                curr -= abs(ord(s[l]) - ord(t[l]))\n                l += 1\n            res = max(res, (r - l + 1))\n        return res\n```\n```C []\nint equalSubstring(char* s, char* t, int maxCost) {\n    int l = 0, curr = 0, cnt = 0;\n    for (int r = 0; s[r]; r++) {\n        curr += fabs(s[r] - t[r]);\n        while (curr > maxCost) {\n            curr -= fabs(s[l] - t[l]);\n            l++;\n        }\n        cnt = fmax(cnt, (r - l + 1));\n    }\n    return cnt;\n}\n```\n```Java []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int l = 0, curr = 0, cnt = 0;\n        for (int r = 0; r < s.length(); r++) {\n            curr += Math.abs(s.charAt(r) - t.charAt(r));\n            while (curr > maxCost) {\n                curr -= Math.abs(s.charAt(l) - t.charAt(l));\n                l++;\n            }\n            cnt = Math.max(cnt, (r - l + 1));\n        }\n        return cnt;\n    }\n}\n```\n```python []\nclass Solution(object):\n    def equalSubstring(self, s, t, maxCost):\n        """\n        :type s: str\n        :type t: str\n        :type maxCost: int\n        :rtype: int\n        """\n        n = len(s)\n        cost = [abs(ord(s[i]) - ord(t[i])) for i in range(n)]\n        \n        left = 0\n        total_cost = 0\n        max_length = 0\n        \n        for right in range(n):\n            total_cost += cost[right]\n            \n            while total_cost > maxCost:\n                total_cost -= cost[left]\n                left += 1\n            \n            max_length = max(max_length, right - left + 1)\n        \n        return max_length\n```\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n```\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int l = 0, curr = 0, cnt = 0;\n        for (int r = 0; r < s.length(); r++) {\n            curr += Math.abs(s.charAt(r) - t.charAt(r));\n            while (curr > maxCost) {\n                curr -= Math.abs(s.charAt(l) - t.charAt(l));\n                l++;\n            }\n            cnt = Math.max(cnt, (r - l + 1));\n        }\n        return cnt;\n    }\n}\n```\n![th.jpeg](https://assets.leetcode.com/users/images/1c7c196f-fbec-4dfa-baeb-e169b5e1b497_1716868193.7449622.jpeg)\n
['String', 'Binary Search', 'C', 'Sliding Window', 'Prefix Sum', 'Python', 'C++', 'Java', 'Python3', 'C#']