Two pointers simple explanation with clean code
Intuition\n- We can store difference of each character from two strings in a separate list. \n- Now our problem statemenet reduces to find maximum number of ele
# Intuition\n- We can store difference of each character from two strings in a separate list. \n- Now our problem statemenet reduces to find maximum number of elements you can pick such that their sum does not exceed m we can use sliding window to solve this\n- Instead of storing difference in a separate list we can find this inline saving us extra space\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- We use `beg` as first pointer of elements and `sum` will store sum of all elements till `res` will have maximum elements\n- Maintain two pointers `beg` and `i` add current difference to sum\n- Now move `beg` till the sum is greater than `m` so that the subsequence is valid\n- update result if this subsequence has more elements than res\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n) we doing single pass on our list only\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: Constant Space no extra space needed\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int m) {\n        int res = 0,beg=0,sum=0;\n        for(int i=0;i<s.size();i++) {\n            sum+=abs(s[i]-t[i]);\n            while(sum>m) {\n                sum-=abs(s[beg]-t[beg]);\n                beg++;\n            }\n            res = max(res, i-beg+1);\n        }\n        return res;\n    }\n};\n```
['C++']