medium

Get Equal Substrings Within Budget

Find the longest substring you can change into another string within a total cost budget.

1. Define the problem

Get Equal Substrings Within Budget

You are given two strings s and t of the same length and an integer maxCost. The cost of changing one character of s into the matching character of t is the absolute difference between their character codes. Return the maximum length of a substring of s you can convert to match t without the total cost exceeding maxCost . Grow a window on the right adding its conversion cost, then shrink from the left whenever the total cost goes over budget.

Constraints

  • 1 ≤ s.length == t.length ≤ 105
  • 0 ≤ maxCost ≤ 106
  • s and t consist of only lowercase English letters

Example

Inputs = "abcd", t = "bcdf", maxCost = 3
Output3

Explanation Changing "abc" to "bcd" costs 1 + 1 + 1 = 3, within budget; including the final d pushes the cost to 5.

2. Know the words first

In plain terms

Character code
The number a computer uses to represent a character internally — 'a' is 97 and 'b' is 98, so the cost to change one into the other is 1.
3. Visualize the solution

Shrink left whenever the cost exceeds the budget

Shrink left whenever the cost exceeds the budget
Statusvalid

Costs 1+1+1=3 <= 3; best = 3.

What happens in this step

s="abcd", t="bcdf" — per-char cost = |charCode(s[i]) - charCode(t[i])|
  index 0: |a-b| = 1
  index 1: |b-c| = 1
  index 2: |c-d| = 1
  running cost = 1+1+1 = 3 <= maxCost=3 → valid
  best = max(0, 2-0+1) = 3
Step 1 of 3

Steps to visualize

  1. Grow right and add the cost of changing that character into the running total.
  2. While the total cost exceeds maxCost, subtract the leftmost cost and advance left.
  3. After each adjustment, the window represents a substring convertible within budget.
  4. Track the longest such window seen.
  5. Continue until right reaches the end of the string.
4. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Shrink left whenever the cost exceeds the budget
Statusvalid

Costs 1+1+1=3 <= 3; best = 3.

What happens in this step

s="abcd", t="bcdf" — per-char cost = |charCode(s[i]) - charCode(t[i])|
  index 0: |a-b| = 1
  index 1: |b-c| = 1
  index 2: |c-d| = 1
  running cost = 1+1+1 = 3 <= maxCost=3 → valid
  best = max(0, 2-0+1) = 3
Step 1 of 3
5. Solution

Solution

solution.tsTypeScript
function equalSubstring(s, t, maxCost) {
  let left = 0;
  let cost = 0;
  let best = 0;

  for (let right = 0; right < s.length; right++) {
    cost += Math.abs(s.charCodeAt(right) - t.charCodeAt(right));

    while (cost > maxCost) {
      cost -= Math.abs(s.charCodeAt(left) - t.charCodeAt(left));
      left++;
    }

    best = Math.max(best, right - left + 1);
  }

  return best;
}
Time
O(n)
Space
O(1)
6. Test cases

Test cases

InputExpectedCovers
s = "abcd", t = "bcdf", maxCost = 33Docstring example
s = "aaaa", t = "bbbb", maxCost = 00maxCost = 0 with no already-equal characters
s = "abcd", t = "bcde", maxCost = 1004Budget large enough to cover everything
s = "a", t = "z", maxCost = 10A single character's own cost exceeds the budget
s = "aabb", t = "aabb", maxCost = 04Every character already matches
s = "a", t = "a", maxCost = 01Single already-equal character
s = "krrgw", t = "zjxss", maxCost = 192Multiple shrink cycles across the window

Keep reading

TopicDescription
Sliding WindowSolve fixed-window problems: maximum sum of k elements, moving averages, and O(n) updates as the window slides.
Maximum Average Subarray IFind the contiguous subarray of a fixed length with the largest average.
Longest Substring Without Repeating CharactersFind the length of the longest run of characters in a string with no repeats.
Permutation in StringCheck whether one string contains a rearrangement of another string as a substring.
Minimum Size Subarray SumFind the shortest contiguous subarray whose sum is at least a target value.
Longest Repeating Character ReplacementFind the longest substring you can turn into one repeated letter with a limited number of swaps.