Skip to content
Decoder

Get Equal Substrings Within Budget

Medium

Problem

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`.

Example 1

Input
s = "abcd", t = "bcdf", max_cost = 3
Output
3
Why
"abc" → "bcd" costs 3

Example 2

Input
s = "abcd", t = "cdef", max_cost = 3
Output
1

Example 3

Input
s = "abcd", t = "acde", max_cost = 0
Output
1
Why
"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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.