↓Skip to main content
  1. LeetCode/

LeetCode 1208: Get Equal Substrings Within Budget

·1 min· ·
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-10

  • Submissions: Accepted (first try)

Approach
#

Position i costs |ord(s[i]) - ord(t[i])|, always non-negative. The problem becomes: the longest subarray of costs with sum <= maxCost.

With non-negative costs, a larger window has a larger sum, so the condition is monotonic: use the “longest” sliding window.

My Solution
#

class Solution:
    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
        left = cost = best = 0
        for right in range(len(s)):
            cost += abs(ord(s[right]) - ord(t[right]))
            while cost > maxCost:
                cost -= abs(ord(s[left]) - ord(t[left]))
                left += 1
            best = max(best, right - left + 1)
        return best

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • With negative costs a sliding window would not work, but these are absolute values, so they are never negative.

Interview Explanation
#

Turning each position into a cost |s[i] - t[i]| makes this the longest subarray with sum at most maxCost. Costs are non-negative, so a sliding window works: shrink the left when over budget and track the longest length. Time O(n).

Related

LeetCode 2024: Maximize the Confusion of an Exam
·2 mins
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 2024 note: a window is valid when the rarer of T and F appears at most k times (flip all of them); shrink the left when it is not
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 1888: Minimum Number of Flips to Make the Binary String Alternating
·2 mins
LeetCode Medium Sliding-Window String Dynamic-Programming
LeetCode 1888 note: rotations become length-n windows over s + s; the window’s mismatches against 0101… are diff, and against 1010… they are n - diff