Easy Sliding Window Solution
\n# Code\n\nclass Solution {\npublic:\n    static int equalSubstring(string& s, string& t, int maxCost) {\n        const int n=s.size();\n        int l=0, r;// 
\n# Code\n```\nclass Solution {\npublic:\n    static int equalSubstring(string& s, string& t, int maxCost) {\n        const int n=s.size();\n        int l=0, r;// 2 pointers\n        int cost=0, len=0;\n\n        // Initialize the window by moving r while cost<=maxCost\n        for (r = 0; r < n; r++) {\n            cost+=abs(s[r]-t[r]);\n            if (cost>maxCost) {\n                cost-=abs(s[r]-t[r]);\n                break;\n            }\n        }\n\n        // If the initial window exceeds the whole string\n        if (r==n && cost<=maxCost) return n;\n        \n        len=r; //initial length for the valid window\n\n        // Sliding the window\n        for (; r<n; r++) {\n            cost+=abs(s[r]-t[r]);\n            while (cost > maxCost) {\n                cost-=abs(s[l]-t[l]);\n                l++;\n            }\n            len=max(len, r-l+1);\n        }\n        return len;\n    }\n};\n\n\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n\n![Cat asking for an Upvote.jpeg](https://assets.leetcode.com/users/images/482d8011-0e7d-4458-9349-8ff704a1aad0_1716962442.0478902.jpeg)\n
['String', 'Sliding Window', 'Prefix Sum', 'C++']