Sliding Window Technique || beats 93% users with C++ & 85% with java || python3 || typescript
Approach\nSliding Window Technique\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Explanation\n1. Initialize Variables:\n\n- n: Lengt
# Approach\nSliding Window Technique\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Explanation\n1. **Initialize Variables:**\n\n- n: Length of the strings s and t.\n- maxLength: To store the maximum length of the valid substring found.\n- currentCost: To track the total cost of the current window.\n- left: The left pointer of the sliding window.\n2. **Expand the Window:**\n\n- Iterate through the string s using the right pointer.\n- For each character at position right, calculate the cost to change s.charAt(right) to t.charAt(right) and add it to currentCost.\n3. **Shrink the Window if Needed:**\n\n- If currentCost exceeds maxCost, increment the left pointer to reduce the window size until currentCost is less than or equal to maxCost.\n4. **Update Maximum Length:**\n\n- For each valid window, update maxLength with the size of the current window, which is right - left + 1.\n5. **Return Result:**\n\n- Finally, return maxLength, which holds the maximum length of the substring that can be transformed within the given cost.\n\n---\n\n```java []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        int n = s.length();\n        int maxLength = 0;\n        int currentCost = 0;\n        int left = 0;\n        \n        for (int right = 0; right < n; right++) {\n            // Calculate the cost of changing s[right] to t[right]\n            currentCost += Math.abs(s.charAt(right) - t.charAt(right));\n            \n            // While currentCost exceeds maxCost, move the left pointer to the right\n            while (currentCost > maxCost) {\n                currentCost -= Math.abs(s.charAt(left) - t.charAt(left));\n                left++;\n            }\n            \n            // Update the maximum length of the substring found so far\n            maxLength = Math.max(maxLength, right - left + 1);\n        }\n        \n        return maxLength;\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int n = s.length();\n        int maxLength = 0;\n        int currentCost = 0;\n        int left = 0;\n\n        for (int right = 0; right < n; right++) {\n            // Calculate the cost of changing s[right] to t[right]\n            currentCost += abs(s[right] - t[right]);\n\n            // While currentCost exceeds maxCost, move the left pointer to the right\n            while (currentCost > maxCost) {\n                currentCost -= abs(s[left] - t[left]);\n                left++;\n            }\n\n            // Update the maximum length of the substring found so far\n            maxLength = max(maxLength, right - left + 1);\n        }\n\n        return maxLength;\n    }\n};\n\n```\n```python3 []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        n = len(s)\n        max_length = 0\n        current_cost = 0\n        left = 0\n        \n        for right in range(n):\n            # Calculate the cost of changing s[right] to t[right]\n            current_cost += abs(ord(s[right]) - ord(t[right]))\n            \n            # While current_cost exceeds maxCost, move the left pointer to the right\n            while current_cost > maxCost:\n                current_cost -= abs(ord(s[left]) - ord(t[left]))\n                left += 1\n            \n            # Update the maximum length of the substring found so far\n            max_length = max(max_length, right - left + 1)\n        \n        return max_length\n\n```\n```Typescript []\nfunction equalSubstring(s: string, t: string, maxCost: number): number {\n    let n = s.length;\n    let maxLength = 0;\n    let currentCost = 0;\n    let left = 0;\n\n    for (let right = 0; right < n; right++) {\n        // Calculate the cost of changing s[right] to t[right]\n        currentCost += Math.abs(s.charCodeAt(right) - t.charCodeAt(right));\n\n        // While currentCost exceeds maxCost, move the left pointer to the right\n        while (currentCost > maxCost) {\n            currentCost -= Math.abs(s.charCodeAt(left) - t.charCodeAt(left));\n            left++;\n        }\n\n        // Update the maximum length of the substring found so far\n        maxLength = Math.max(maxLength, right - left + 1);\n    }\n\n    return maxLength;\n}\n\n```\n
['String', 'Sliding Window', 'C++', 'Java', 'TypeScript', 'Python3']