✅  Beats 100% | ✅ Working 25.10.2024
python3 []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        # Get length of input strings (they are guaranteed to b
```python3 []\nclass Solution:\n    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n        # Get length of input strings (they are guaranteed to be equal length)\n        n = len(s)\n        \n        # Create array of transformation costs between characters at each position\n        # ord() converts character to ASCII value, abs() gets positive difference\n        costs = [abs(ord(s[i]) - ord(t[i])) for i in range(n)]\n        \n        # Initialize variables for sliding window approach:\n        curr_sum = 0    # Tracks current cost within window\n        max_length = 0  # Stores maximum valid window length found\n        left = 0        # Left pointer of sliding window\n        \n        # Right pointer moves through string, expanding window\n        for right in range(n):\n            # Add cost of new character to window sum\n            curr_sum += costs[right]\n            \n            # If window cost exceeds maxCost, shrink window from left\n            # until we\'re back under maxCost\n            while curr_sum > maxCost and left <= right:\n                curr_sum -= costs[left]\n                left += 1\n            \n            # Current window is valid, update max_length if current window is larger\n            # Window size is (right - left + 1)\n            max_length = max(max_length, right - left + 1)\n        \n        # Return the length of longest valid substring found\n        return max_length\n```\n```cpp []\nclass Solution {\npublic:\n    // Function to find the longest substring that can be transformed within maxCost\n    static int equalSubstring(string& s, string& t, int maxCost) {\n        // Length of input strings\n        const int n = s.size();\n        \n        // Two pointers for sliding window\n        int l = 0, r;\n        \n        // Track current cost and max length found\n        int cost = 0, len = 0;\n\n        // First pass: Try to include as many characters as possible from start\n        for (r = 0; r < n; r++) {\n            // Add cost of transforming current character\n            cost += abs(s[r]-t[r]);\n            \n            // If cost exceeds maxCost, undo last addition and break\n            if (cost > maxCost) {\n                cost -= abs(s[r]-t[r]);\n                break;\n            }\n        }\n\n        // If we reached end of string and cost is acceptable,\n        // entire string can be transformed\n        if (r == n && cost <= maxCost) return n;\n        \n        // Store the length of first valid window\n        len = r;\n\n        // Second pass: Sliding window approach\n        for (; r < n; r++) {\n            // Add cost of including new character\n            cost += abs(s[r]-t[r]);\n            \n            // While cost exceeds maxCost, shrink window from left\n            while (cost > maxCost) {\n                cost -= abs(s[l]-t[l]);\n                l++;\n            }\n            \n            // Update maximum length if current window is larger\n            len = max(len, r-l+1);\n        }\n        \n        return len;\n    }\n};\n\n// Lambda function for I/O optimization\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n```java []\nclass Solution {\n    public int equalSubstring(String s, String t, int maxCost) {\n        // Length of input strings\n        final int n = s.length();\n        \n        // Two pointers for sliding window\n        int l = 0, r;\n        \n        // Track current cost and max length found\n        int cost = 0, len = 0;\n        \n        // First pass: Try to include as many characters as possible from start\n        for (r = 0; r < n; r++) {\n            // Add cost of transforming current character\n            cost += Math.abs(s.charAt(r) - t.charAt(r));\n            \n            // If cost exceeds maxCost, undo last addition and break\n            if (cost > maxCost) {\n                cost -= Math.abs(s.charAt(r) - t.charAt(r));\n                break;\n            }\n        }\n        \n        // If we reached end of string and cost is acceptable,\n        // entire string can be transformed\n        if (r == n && cost <= maxCost) return n;\n        \n        // Store the length of first valid window\n        len = r;\n        \n        // Second pass: Sliding window approach\n        for (; r < n; r++) {\n            // Add cost of including new character\n            cost += Math.abs(s.charAt(r) - t.charAt(r));\n            \n            // While cost exceeds maxCost, shrink window from left\n            while (cost > maxCost) {\n                cost -= Math.abs(s.charAt(l) - t.charAt(l));\n                l++;\n            }\n            \n            // Update maximum length if current window is larger\n            len = Math.max(len, r - l + 1);\n        }\n        \n        return len;\n    }\n}\n```\n```csharp []\npublic class Solution {\n    public int EqualSubstring(string s, string t, int maxCost) {\n        // Length of input strings\n        int n = s.Length;\n        \n        // Two pointers for sliding window\n        int l = 0, r;\n        \n        // Track current cost and max length found\n        int cost = 0, len = 0;\n        \n        // First pass: Try to include as many characters as possible from start\n        for (r = 0; r < n; r++) {\n            // Add cost of transforming current character\n            cost += Math.Abs(s[r] - t[r]);\n            \n            // If cost exceeds maxCost, undo last addition and break\n            if (cost > maxCost) {\n                cost -= Math.Abs(s[r] - t[r]);\n                break;\n            }\n        }\n        \n        // If we reached end of string and cost is acceptable,\n        // entire string can be transformed\n        if (r == n && cost <= maxCost) return n;\n        \n        // Store the length of first valid window\n        len = r;\n        \n        // Second pass: Sliding window approach\n        for (; r < n; r++) {\n            // Add cost of including new character\n            cost += Math.Abs(s[r] - t[r]);\n            \n            // While cost exceeds maxCost, shrink window from left\n            while (cost > maxCost) {\n                cost -= Math.Abs(s[l] - t[l]);\n                l++;\n            }\n            \n            // Update maximum length if current window is larger\n            len = Math.Max(len, r - l + 1);\n        }\n        \n        return len;\n    }\n}\n```\n```golang []\npackage solution\n\ntype Solution struct{}\n\nfunc (s *Solution) equalSubstring(s string, t string, maxCost int) int {\n    n := len(s)\n    l, r := 0, 0\n    cost := 0\n    length := 0\n\n    // First pass: Try to include as many characters as possible from start\n    for r = 0; r < n; r++ {\n        cost += abs(int(s[r]) - int(t[r]))\n        \n        if cost > maxCost {\n            cost -= abs(int(s[r]) - int(t[r]))\n            break\n        }\n    }\n\n    if r == n && cost <= maxCost {\n        return n\n    }\n    \n    length = r\n\n    // Second pass: Sliding window approach\n    for ; r < n; r++ {\n        cost += abs(int(s[r]) - int(t[r]))\n        \n        for cost > maxCost {\n            cost -= abs(int(s[l]) - int(t[l]))\n            l++\n        }\n        \n        if r-l+1 > length {\n            length = r - l + 1\n        }\n    }\n    \n    return length\n}\n\nfunc abs(x int) int {\n    if x < 0 {\n        return -x\n    }\n    return x\n}\n```\n```php []\nclass Solution {\n    /**\n     * @param String $s\n     * @param String $t\n     * @param Integer $maxCost\n     * @return Integer\n     */\n    function equalSubstring($s, $t, $maxCost) {\n        // Length of input strings\n        $n = strlen($s);\n        \n        // Two pointers for sliding window\n        $l = 0;\n        \n        // Track current cost and max length found\n        $cost = 0;\n        $len = 0;\n        \n        // First pass: Try to include as many characters as possible from start\n        for ($r = 0; $r < $n; $r++) {\n            // Add cost of transforming current character\n            $cost += abs(ord($s[$r]) - ord($t[$r]));\n            \n            // If cost exceeds maxCost, undo last addition and break\n            if ($cost > $maxCost) {\n                $cost -= abs(ord($s[$r]) - ord($t[$r]));\n                break;\n            }\n        }\n        \n        // If we reached end of string and cost is acceptable,\n        // entire string can be transformed\n        if ($r == $n && $cost <= $maxCost) return $n;\n        \n        // Store the length of first valid window\n        $len = $r;\n        \n        // Second pass: Sliding window approach\n        for (; $r < $n; $r++) {\n            // Add cost of including new character\n            $cost += abs(ord($s[$r]) - ord($t[$r]));\n            \n            // While cost exceeds maxCost, shrink window from left\n            while ($cost > $maxCost) {\n                $cost -= abs(ord($s[$l]) - ord($t[$l]));\n                $l++;\n            }\n            \n            // Update maximum length if current window is larger\n            $len = max($len, $r - $l + 1);\n        }\n        \n        return $len;\n    }\n}\n```\n### Complexity \n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n![image.png](https://assets.leetcode.com/users/images/9dc1b265-b175-4bf4-bc6c-4b188cb79220_1728176037.4402142.png)
['PHP', 'C++', 'Java', 'Go', 'Python3', 'C#']