Simple O(n)
IntuitionThe cost is defined by diff of chars at corresponding index of both the strings.ApproachCalculate cost of each index by taking difference and store the
# Intuition

The cost is defined by diff of chars at corresponding index of both the strings.

# Approach
Calculate cost of each index by taking difference and store them in a vector. Now in the vector, look for longest subset with sum less than or equal to maxCost.

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(n) -> for storing cost at all indices

# Code
```cpp []
class Solution {
public:
    int equalSubstring(string s, string t, int maxCost) {
        vector<int> cost (s.size());
        for (int i=0; i<s.size(); i++){
            int diff = abs (s[i]-t[i]);
            cost[i] = diff;
        }
        int start =0;
        int curr =0;
        int ans =0;
        for (int i =0; i<cost.size(); i++){
            curr += cost[i];
            if (curr<=maxCost){
                ans = max(ans, i-start+1);
            }
            if (curr>maxCost){
                while (start<cost.size() && curr>maxCost){
                    curr -= cost[start];
                    start++;
                }
            }
        }
        return ans;
    }
};
```
['Sliding Window', 'C++']