↓Skip to main content
  1. LeetCode/

LeetCode 4026: Maximum Gap Between Stations

·2 mins· ·
LeetCode Medium Two-Pointers Greedy String
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-08-26

  • Submissions: Wrong Answer → Accepted

Approach
#

Workers must take stations in increasing index order with skill[i] == station[j], which means matching skill as a subsequence of station.

To maximize the gap between workers i and i + 1:

  • Push workers 0..i as far left as possible: a greedy left-to-right match gives each worker’s earliest station earliest[i].
  • Push workers i + 1..n - 1 as far right as possible: a greedy right-to-left match gives the latest station latest[i + 1].

The two sides do not interfere (earliest[i] < latest[i + 1], since a valid assignment is guaranteed), so the gap latest[i + 1] - earliest[i] is achievable. The answer is its maximum over all i.

Both greedy matches walk two same-direction pointers over skill and station, like a subsequence check.

My Attempts
#

Attempt 1 built the right-side list by appending from the last worker backwards, so latest was reversed, but it used latest[i + 1] directly. For skill = "ab", station = "aabb", the earliest a is at 0 and the latest b at 3, so the answer is 3; this returned 1.

Adding right.reverse() passed.

Solution
#

class Solution:
    def maximumGap(self, skill: str, station: str) -> int:
        n, m = len(skill), len(station)
        earliest = [0] * n
        j = 0
        for i in range(n):
            while station[j] != skill[i]:
                j += 1
            earliest[i] = j                        # earliest station for worker i
            j += 1

        latest = [0] * n
        j = m - 1
        for i in range(n - 1, -1, -1):
            while station[j] != skill[i]:
                j -= 1
            latest[i] = j                          # latest station for worker i
            j -= 1

        return max((latest[i + 1] - earliest[i] for i in range(n - 1)), default=0)

Writing latest[i] by index avoids the reversal. With one worker, default=0.

Complexity
#

Time O(n + m), space O(n).

Mistakes to Avoid
#

  • A list appended from right to left is reversed.
  • With one worker the answer is 0.

Interview Explanation
#

Assigning stations is matching skill as a subsequence of station. To maximize the gap between two neighboring workers, the earlier workers go as far left and the later ones as far right as possible. I greedily match from the left for each worker’s earliest station and from the right for the latest, then take the maximum of latest[i + 1] - earliest[i] over the split points. Both passes are same-direction pointers, O(n + m).

Related

LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking
LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one
LeetCode 2337: Move Pieces to Obtain a String
·2 mins
LeetCode Medium Two-Pointers String
LeetCode 2337 note: after skipping blanks the piece order must match; L only moves left and R only moves right, so each L’s start index must be >= its target index and each R’s must be <=
LeetCode 777: Swap Adjacent in LR String
·2 mins
LeetCode Medium Two-Pointers String
LeetCode 777 note: XL→LX moves L left and RX→XR moves R right; after skipping X the order must match, with L only moving left and R only moving right
LeetCode 809: Expressive Words
·3 mins
LeetCode Medium Two-Pointers String Array
LeetCode 809 note: two pointers compare runs of equal characters; when the lengths differ, the run in s must be at least 3 and no shorter than the word’s run, and both strings must end together
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·3 mins
LeetCode Medium Two-Pointers Greedy Array
LeetCode 1989 note: process the “it” players left to right, each catching the leftmost uncaught person in range, with a right-only pointer to the next candidate