Get Equal Substrings Within Budget

Medium

Description

You are given two strings s and t of the same length and an integer maxCost. You want to change s to t. The cost of changing s[i] to t[i] is |s[i] - t[i]|. Return the maximum length of a substring that can be changed with cost at most maxCost.

Examples

Input:s = "abcd", t = "bcdf", maxCost = 3
Output:3
Explanation:

abc -> bcd costs 3.

Input:s = "a", t = "a", maxCost = 3
Output:0
Explanation:

Edge case with a single character.

Input:s = "abcdef", t = "aecdhf", maxCost = 4
Output:4
Explanation:

The costs for each position are: |a-a|=0, |b-e|=3, |c-c|=0, |d-d|=0, |e-h|=3, |f-f|=0. It is possible to choose substring "bcde" (positions 1-4) with total cost 3+0+0+3=6, but this exceeds maxCost. The optimal substring is "abcd" (positions 0-3) with cost 0+3+0+0=3, giving us length 4.

Constraints

  • 1 ≤ s.length ≤ 10^5
  • s.length == t.length

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!