> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/two-pointer/sliding-window/1208.-get-equal-substrings-within-budget.md).

# 1208. Get Equal Substrings Within Budget

```
T: O(n)
S: O(1)
```

```java
class Solution {
    public int equalSubstring(String s, String t, int maxCost) {
        int sum = 0;
        int left = 0;
        int result = 0;
        for (int right = 0; right < s.length(); right++) {
            sum += Math.abs((s.charAt(right) - 'a') - (t.charAt(right) - 'a'));;
            while (left <= right && sum > maxCost) {
                sum -= Math.abs((s.charAt(left) - 'a') - (t.charAt(left) - 'a'));;
                left++;
            }
            result = Math.max(result, right - left + 1);
        }
        return result;
    }
}

/**
maxCost = 0
 x 
 r  
abcd
bcdf
  l
r - l + 1 = 0

   r  
abcd
bcdf
    l

end

T: O(n)
S: O(1)
 */
```
