Sliding window solution using Python3!!
Intuition

The goal is to find the maximum length of a contiguous substring of s that can be changed to its corresponding substring in t without the total cost
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
1. ***The goal is to find the maximum length of a contiguous substring of*** `s` ***that can be changed to its corresponding substring in*** `t` ***without the total cost exceeding*** `maxCost`.

1. ***Changing a character*** `s[i]` ***to*** `t[i]` ***costs*** `abs(ord(s[i]) - ord(t[i]))`. ***So, we're essentially trying to find the longest window (substring) where the sum of these costs ≤ maxCost.***

1. ***This type of problem maps perfectly to a [Variable Sliding Window](https://www.codeintuition.io/courses/array/tFob_VIXJQJ8bUN3hlSMB) pattern, where we adjust the window boundaries dynamically based on whether the cost stays within the budget.***

# Approach
<!-- Describe your approach to solving the problem. -->
1. **Use two pointers** `l` **and** `r` **to define a sliding window. Both start at index 0.**
1. **As you iterate through the string using** `r` **(right pointer), calculate the cost of changing** `s[r]` **to** `t[r]` **and accumulate it in** `val`**.**
1. **If the total cost** `val` **becomes greater than** `maxCost`**, shrink the window from the left** `(l += 1)` **and subtract the cost of** `s[l] - t[l]` **from** `val`**, until the window becomes valid again (i.e.,** `val <= maxCost`**).**
1. **At every step, update the result** `res` **with the maximum window length found so far** `(r - l + 1)`**.**
1. **Continue until the end of the string.**

# Complexity
##### - Time complexity:$$O(n)$$
- Each character is visited at most twice — once by the right pointer r and once by the left pointer l. So it's a linear-time solution.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->


##### - Space complexity:$$O(1)$$
- No extra space is used apart from a few variables. Cost is calculated on-the-fly — no need to store cost arrays.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->


# Code
```python3 []
class Solution:
    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
        res,n=0,len(s)
        l,r=0,0
        val=0
        while r<n:
            val+=abs(ord(s[r])-ord(t[r]))
            if val<=maxCost:
                res=max(res,r-l+1)
            else:
                while val>maxCost:
                    val-=abs(ord(s[l])-ord(t[l]))
                    l+=1
            r+=1
        return res
            
```

![upvote leetcode.png](https://assets.leetcode.com/users/images/5d96f06a-53ec-486c-9119-dfa08b587e23_1741523407.475307.png)


['String', 'Sliding Window', 'Python3']