Sliding Window 2 Approaches
Sliding window with Prefix sum\n# Code\n\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int n=s.length();\n     
# Sliding window with Prefix sum\n# Code\n```\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int n=s.length();\n        vector<int>pref(n,0);\n        pref[0]=abs(s[0]-t[0]);\n        for(int i=1;i<n;i++)\n        {\n            pref[i]=pref[i-1]+abs(s[i]-t[i]);\n        }\n        // Why do we need this step \uD83E\uDD14? Can you answer it ?\n        pref.insert(pref.begin(),0);\n        int cost=0,maxLen=0;\n        for(int i=0,j=0;j<n+1;j++)\n        {\n            cost=pref[j]-pref[i];\n            while(i<j and cost>maxCost)\n            {\n                i++;\n                cost=pref[j]-pref[i];\n            }\n            maxLen=max(maxLen,j-i);\n        }\n\n        return maxLen;\n    }\n};\n```\n\n# Space Optimized Sliding Window\n\n```\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxCost) {\n        int n=s.length();\n        int cost=0,maxLen=0;\n        for(int i=0,j=0;j<n;j++)\n        {\n            cost+=abs(s[j]-t[j]);\n            while(i<=j and cost>maxCost)\n            {\n                cost-=abs(s[i]-t[i]);\n                i++;\n            }\n            maxLen=max(maxLen,j-i+1);\n        }\n\n        return maxLen;\n    }\n};\n```
['C++']