Simple C++ Solution || Sliding window || C++
IntuitionWe want to find the maximum length of a substring where the total transformation cost from s[i] to t[i] is less than or equal to maxCost.
If the cost e
---

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We want to find the **maximum length of a substring** where the **total transformation cost from `s[i]` to `t[i]` is less than or equal to `maxCost`**.  
If the cost exceeds `maxCost`, we need to **remove characters from the start of the current window** (sliding window technique).

---

# Approach
<!-- Describe your approach to solving the problem. -->
1. First, calculate a `cost` array which stores `abs(s[i] - t[i])` for each index.
2. Use **two pointers `i` (right) and `j` (left)** to maintain a sliding window.
3. Expand the right pointer `i`, and keep track of the total cost `c` and length `cnt` of current window.
4. If total cost exceeds `maxCost`, **shrink the window from the left** by increasing `j` and updating `c` and `cnt`.
5. Keep updating `len` with the maximum value of `cnt`.

---

# Example:  

Let’s take this example:  
`s = "abcd"`, `t = "bcdf"`, `maxCost = 3`

| Index | s[i] | t[i] | cost[i] = abs(s[i] - t[i]) | Window (i to j) | Total Cost | Comment                        |
|-------|------|------|-----------------------------|------------------|-------------|--------------------------------|
| 0     | a    | b    | 1                           | 0 to 0           | 1           | Within maxCost                |
| 1     | b    | c    | 1                           | 0 to 1           | 2           | Within maxCost                |
| 2     | c    | d    | 1                           | 0 to 2           | 3           | Exactly maxCost               |
| 3     | d    | f    | 2                           | 0 to 3           | 5           | Cost > maxCost → Remove cost[0] (1) |
|       |      |      |                             | 1 to 3           | 4           | Still > maxCost → Remove cost[1] (1) |
|       |      |      |                             | 2 to 3           | 3           | Now valid                     |

Final Answer = Max length = **2** (from index 2 to 3)

---

### Complexity

- **Time complexity:**  
  $$O(n)$$  
  - One pass over the array for building cost.  
  - One pass for sliding window traversal.

- **Space complexity:**  
  $$O(n)$$  
  - Extra space used for the `cost` array.

---

# Code
```cpp []
class Solution {
public:
    int equalSubstring(string s, string t, int maxCost) {
        int n=s.length();
        vector<int>cost;
        for(int i=0;i<n;i++){
            cost.push_back(abs(s[i]-t[i]));
        }
        int len=0,c=0;
        int cnt=0;
        int j=0;
        for(int i=0;i<n;i++){
            c+=cost[i];
            cnt++;
            if(c>maxCost){
                c-=cost[j];
                j++;
                cnt--;
            }
            len=max(len,cnt);
        }
        return len;
    }
};
```
['C++']