Talentd
FleetCode

4026. Maximum Gap Between Stations

Medium52.4% acceptance

You are given two strings skill and station of lengths n and m, respectively.

skill[i] represents the skill of worker i, and station[j] represents the skill supported by station j.

You must assign every worker to a distinct station. Let ji be the index of the station assigned to worker i. A valid assignment must satisfy:

  • station[ji] == skill[i] for every 0 <= i < n.
  • The assigned station indices must be strictly increasing in worker order, meaning j0 < j1 < ... < jn - 1.

The gap of an assignment is the maximum difference between the station indices assigned to two consecutive workers. In other words, it is max(ji - ji - 1) over all 1 <= i < n.

If there is only one worker, the gap is 0.

Return the maximum possible gap among all valid assignments. It is guaranteed that at least one valid assignment exists.

Example 1:

Input: skill = "aa", station = "aaaa"

Output: 3

Explanation:

  • The two workers must be assigned to two different 'a' stations.
  • Assigning them to stations [0, 3] gives a gap of 3.

Example 2:

Input: skill = "xyz", station = "xyzz"

Output: 2

Explanation:

  • Assign worker 0 to station j = 0, and worker 1 to station j = 1.
  • To maximize the gap, assign worker 2 to station j = 3.
  • This gives the assignment [0, 1, 3] with gaps [1, 2], so the gap is 2.

Example 3:

Input: skill = "cbc", station = "cbcdbc"

Output: 4

Explanation:

  • Assign worker 0 to station j = 0, and worker 1 to station j = 1.
  • To maximize the gap, assign worker 2 to station j = 5.
  • This gives the assignment [0, 1, 5] with gaps [1, 4], so the gap is 4.

Constraints:

  • skill.length == n
  • station.length == m
  • 1 <= n <= m <= 105
  • skill and station consist of lowercase English letters.
  • It is guaranteed that a valid assignment exists for every worker.

Hints open one at a time

Hint 1

For each adjacent pair of workers i - 1 and i, try to maximize the distance between their assigned stations while still allowing all earlier and later workers to be matched.

Hint 2

Compute the earliest possible station index for every worker by greedily matching skill from left to right.

Hint 3

Compute the latest possible station index for every worker by greedily matching skill from right to left.

Hint 4

For each 1 <= i < n, workers before i can use their earliest possible positions and workers from i onward can use their latest possible positions. The largest possible gap between workers i - 1 and i is latest[i] - earliest[i - 1].

20:04
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maximumGap(self, skill: str, station: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"aa"
"aaaa"
Input
"xyz"
"xyzz"
Input
"cbc"
"cbcdbc"
Your code is saved on this device.Solution