Skip to content

Get Equal Substrings Within Budget

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

You get two strings s and t of the same length and an integer max_cost. Changing s[i] into t[i] costs abs(ord(s[i]) - ord(t[i])), the distance between their character codes. Return the length of the longest substring of s that can be changed to the matching substring of t with a total cost of at most max_cost.

s = "abcd", t = "bcdf", max_cost = 3   →  3   ("abc" → "bcd" costs 3)
s = "abcd", t = "cdef", max_cost = 3   →  1
s = "abcd", t = "acde", max_cost = 0   →  1   ("a" already matches)

Constraints

  • 1 <= s.length <= 10^5
  • t.length == s.length
  • 0 <= max_cost <= 10^6
  • s and t consist of lowercase English letters.