💯✅🔥Easy ☕Java☕ Solution|| 7 ms ||≧◠‿◠≦✌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is essentially about finding the longest substring in "s" that can be trans
# *Intuition*\n<!-- Describe your first thoughts on how to solve this problem. -->\n*The problem is essentially about finding the longest substring in "s" that can be transformed into the corresponding substring in "t" with a limited cost. The cost is defined as the sum of the absolute differences of the ASCII values of corresponding characters. To solve this, we can use the sliding window technique which helps in maintaining a window that adjusts based on the cost constraint.*\n\n# *Approach*\n<!-- Describe your approach to solving the problem. -->\n1. *Sliding Window Technique:*\n\n- *We maintain two pointers (slow and fast) that represent the current window in s.*\n- *We start with both pointers at the beginning and expand the fast pointer to include more characters until the cost exceeds maxCost.*\n- *If the cost exceeds maxCost, we increment the slow pointer to reduce the window size and thereby reduce the cost.*\n- *During this process, we keep track of the maximum window size that satisfies the cost constraint.*\n2. *Cost Calculation:*\n\n- *The cost is updated dynamically as the window expands or shrinks.*\n- *When fast pointer moves forward, we add the cost of the new character.*\n- *When slow pointer moves forward, we subtract the cost of the character that is left behind.*\n# *Complexity*\n- *Time complexity:*\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n*The time complexity is $$O(n$$), where n is the length of the strings s and t. Each character is processed at most twice, once when the fast pointer moves and once when the slow pointer moves.*\n- *Space complexity:* \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n*The space complexity is $$O(1)$$ because we only use a fixed amount of extra space for variables such as pointers and the cost accumulator.*\n# *Code*\n```\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int slow = -1, fast = 0;                     \n        int n = s.length(),res = 0,cost = Math.abs(s.charAt(fast)-t.charAt(fast));\n        boolean flag = true;\n        while(slow<=fast&&fast<n){\n            if(cost<=maxCost){\n                res = Math.max(res,fast-slow);\n                fast++;\n                if(fast==n) break;\n                cost+=Math.abs(s.charAt(fast)-t.charAt(fast));\n                continue;\n            }\n            if(cost>maxCost){\n                slow++;\n                cost-=Math.abs(s.charAt(slow)-t.charAt(slow));\n                continue;\n            }\n        }\n        return res;\n    }\n}\n```\n\n![pleaseUpvoteLC.jpeg](https://assets.leetcode.com/users/images/5b845ba3-9cae-4a16-bc81-34db90ff5fcc_1716865229.137788.jpeg)\n
['String', 'Sliding Window', 'Java']