💯🔥Constant space O(1)🎉||✅ DRY RUN || ♻️ Step wise explanation || 2 Approach💥|| 4 Lang Solution
Screenshot \uD83C\uDF89 \n\n\n\n\n# Intuition \uD83E\uDD14\n Describe your first thoughts on how to solve this problem. \n    Given--> \n    \n    1 Two string 
# Screenshot \uD83C\uDF89 \n\n![1208.png](https://assets.leetcode.com/users/images/46207fbc-91a8-435c-a81d-4f3f0799b3af_1716875932.2448285.png)\n\n\n# Intuition \uD83E\uDD14\n<!-- Describe your first thoughts on how to solve this problem. -->\n    `Given`--> \n    \n    1 Two string name (s) And (t) with same length\n\n    2 And a maxCost which tells us cost\n\n---\n\n    `Que description` -->\n\n    we have to maximize the length of subStr and return the len of str\n\n1. `subString` must be `same for both` the given subString\n\n2. we can `change the chars of s` to **match** the `of t\'s subString` \n\n3. And we need to `maximize` this subString length\n\n4. Now catch is the `cost of changing a cha`r of a s is -->\n\n5. `Absolute diff of ascii value of char of t - char of s at Ith pos`\n \n6. ``` java\n    Math.abs(t.charAt(i) - s.charAt(i))  --> BY JAVA\n     ```\n\n7. And we `can\'t afford cost above given maxCost`\n---\n    `Ex 1`\n\n        s = "abcd"  & t = "bcdf" maxCost = 3\n\n        Ans =  3 because "abc" can be change to "bcd" \n\n        \'b\' - \'a\' == 1\n        \'c\' - \'b\' == 1   => Total cost == 3 equal to maxCost\n        \'d\' - \'c\' == 1\n\n`I guess all of you got the idea`\n\n\n# Approach \uD83D\uDE0D\n<!-- Describe your approach to solving the problem. -->\n\n    Approach is simple and we\'ll use no space nothing at all\n\n`First Approach` \n\n## 1. Brute force Approach  \uD83D\uDE0F\n\n    1. Make a array of all the abs diff of char at ith for s and t \n\n    2. And then check for the subArrays if they fit in our solution or not \n\n    \uD83E\uDD70 I can weste your time to continue this appproch \n\n    But i don\'t wanna weste your time so \n\n    let\'s jump to our main sol\n\n `Second Approach` \n\n\n##  2. No Extra space Approach \uD83D\uDE80\uD83D\uDE80\n\n    1. Take two pointer Left assign with 0 and right for loop\n\n    2. We\'ll go in whole string  // In any string length\n\n    3. and store the cost of each time one char change\n\n    4. and add to our curCost from right pointer \n\n    5. If at some pos our curCost > maxCost \n\n    6. inc our left pointer and dec the values from left in curCost\n\n    7. store the max of subStringlen from (right - left + 1) and (Itself)\n\n# Under Stand with code \uD83E\uDEF0\n\n    LET\'s understand with code first then `we will dry run this`\n\n`Step 1` **make variables**\n\n``` PYTHON []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        curCost = 0\n        subStringLen = 0\n\n        l = 0 # left \n```\n```JAVA []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int curCost = 0;\n        int subStringLen = 0;\n\n        int left = 0;\n\n```\n```JAVASCRIPT []\nvar equalSubstring = function (s, t, maxCost) {\n    let curCost = 0\n    let subStringLen = 0\n\n    let left = 0\n```\n```C []\nint equalSubstring(char* s, char* t, int maxCost) {\n    int curCost = 0;\n    int subStringLen = 0;\n\n    int left = 0;\n\n```\n`Step 2` **Apply loop and extract our cost of each index**\n\n- Becaue we `extract the diff at same time` \n-    that\'s `why we don\'t need arr`\n\n``` PYTHON []\n    \n    for right in range(len(s)): \n        curCost += abs(ord(s[right]) - ord(t[right]))\n\n```\n```JAVA []\n    for (int right = 0; right < s.length(); right++) {\n        curCost += Math.abs(s.charAt(right) - t.charAt(right));\n\n    }\n\n```\n```JAVASCRIPT []\n    for (let right = 0; right < s.length; right++) {\n        curCost += Math.abs(\n            s.charAt(right).charCodeAt(0) - \n                        t.charAt(right).charCodeAt(0)\n            );\n```\n```C []\n    for (int right = 0; right < strlen(s); right++) {\n        curCost += abs(s[right] - t[right]);\n\n    }\n\n```\n\n`Step 3` **subtract starting value if curCost > maxCost** \n\n- We\'ll apply `while loop for this` and `inc our left pointer`\n\n``` PYTHON []\n\n    while curCost > maxCost:\n        curCost -= abs(ord(s[left]) - ord(t[l]))\n\n        lelf += 1\n```\n```JAVA []\n\n    while (curCost > maxCost) {\n        curCost -= Math.abs(s.charAt(left) - t.charAt(left));\n        left++;\n    }\n\n```\n```JAVASCRIPT []\n\n    while (curCost > maxCost) {\n        curCost -= Math.abs(s.charAt(left).charCodeAt(0) - t.charAt(left).charCodeAt(0));\n\n        left++;\n    }\n```\n```C []\n\n    while (curCost > maxCost) {\n        curCost -= abs(s[left] - t[left]);\n        left++;\n    }\n```\n\n`Step 4` Now `Store the max len` and return it\n\n    We will count our len in the loop and return to main fun\n\n``` PYTHON []\n    \n            subStringLen = max(subStringLen, r - l + 1)\n\n        return subStringLen\n```\n```JAVA []\n            subStringLen = Math.max(subStringLen, right - left + 1);\n        }\n\n        return subStringLen;\n    }\n}\n\n```\n```JAVASCRIPT []\n        subStringLen = Math.max(subStringLen, right - left + 1);\n    }\n\n    return subStringLen;\n};\n```\n```C []\n  \n        subStringLen = fmax(subStringLen, right - left + 1);\n    }\n\n    return subStringLen;\n}\n\n``` \n\n# DRY RUN \u2622\uFE0F\n\n                         `Taking same example` \n\n---\n\n\n    `Ex`\n\n        s = "abcd"  & t = "bcdf" maxCost = 3\n\n        subStrinLen = 0\n\n\n\n```\nIn the loop \ns = "abcd" \n     ^ l = 0  \n                  curCost = 1   subStrinLen = 1\n     | r = 0  \n```\n\n```\nIn the loop\ns = "abcd" \n     ^ l = 0  \n                  curCost = 2   subStrinLen = 2\n      ^  \n      | r = 1  \n```\n```\nIn for the loop\ns = "abcd" \n     ^ l = 0  \n                  curCost = 3   subStrinLen = 3\n       ^  \n       | r = 2 \n```\n\n```\nIn for the loop\ns = "abcd" \n     ^ l = 0  \n                  curCost = 5   subStrinLen = 3\n        ^  \n        | r = 3 \n\n    In while loop \n        s = "abcd" \n              ^ l = 1  \n                        curCost = 5 - 1 = 4   subStrinLen = 3\n               ^  \n               | r = 3 \n        ```\n    \n    In while loop \n        s = "abcd" \n               ^ l = 2  \n                        curCost = 4 - 1 = 3   subStrinLen = 3\n               ^  \n               | r = 3 \n        ```\n    \n```\n                Loop Ended Max length == subStringLen \n\n---\n\n# Complexity \u2603\uFE0F\n- Time complexity: $$O(n)$$ \u267B\uFE0F\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$ \uD83D\uDD25\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n\n# Code \uD83D\uDC96\n``` PYTHON []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        curCost = 0\n        subStringLen = 0\n\n        l = 0 # left \n\n        for r in range(len(s)): # r == right pointer\n            curCost += abs(ord(s[r]) - ord(t[r]))\n\n            while curCost > maxCost:\n                curCost -= abs(ord(s[l]) - ord(t[l]))\n\n                l += 1\n\n            subStringLen = max(subStringLen, r - l + 1)\n\n        return subStringLen\n```\n```JAVA []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int curCost = 0;\n        int subStringLen = 0;\n\n        int left = 0;\n\n        for (int right = 0; right < s.length(); right++) {\n            curCost += Math.abs(s.charAt(right) - t.charAt(right));\n\n            while (curCost > maxCost) {\n                curCost -= Math.abs(s.charAt(left) - t.charAt(left));\n                left++;\n            }\n\n            subStringLen = Math.max(subStringLen, right - left + 1);\n        }\n\n        return subStringLen;\n    }\n}\n```\n```JAVASCRIPT []\nvar equalSubstring = function (s, t, maxCost) {\n    let curCost = 0\n    let subStringLen = 0\n\n    let left = 0\n\n    for (let right = 0; right < s.length; right++) {\n        curCost += Math.abs(s.charAt(right).charCodeAt(0) - t.charAt(right).charCodeAt(0));\n\n        while (curCost > maxCost) {\n            curCost -= Math.abs(s.charAt(left).charCodeAt(0) - t.charAt(left).charCodeAt(0));\n\n            left++;\n        }\n\n        subStringLen = Math.max(subStringLen, right - left + 1);\n    }\n\n    return subStringLen;\n};\n```\n```C []\nint equalSubstring(char* s, char* t, int maxCost) {\n    int curCost = 0;\n    int subStringLen = 0;\n\n    int left = 0;\n\n    for (int right = 0; right < strlen(s); right++) {\n        curCost += abs(s[right] - t[right]);\n\n        while (curCost > maxCost) {\n            curCost -= abs(s[left] - t[left]);\n            left++;\n        }\n\n        subStringLen = fmax(subStringLen, right - left + 1);\n    }\n\n    return subStringLen;\n}\n```\n\n\n
['String', 'Binary Search', 'C', 'Sliding Window', 'Prefix Sum', 'Java', 'Python3', 'JavaScript']