All problems
0077HardHash TableStringSliding Window

Shortest Kit Stretch

Tracked in this browser only
Write code

Trains the technique from

LeetCode 76Minimum Window Substring

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A conveyor carries parts past an assembly cell in a fixed order. The string belt records the stamp on each part, one letter per part, reading along the belt. A kit order is written as the string kit: every letter in it is a stamp the kit needs, and a letter written twice means the kit needs two parts carrying that stamp. Stamps are case sensitive, so a part stamped b cannot fill a slot that asks for B.

Find the shortest unbroken stretch of the belt that can fill the whole kit and return that stretch as a string. Extra parts inside the stretch are set aside and cost nothing, but a stretch may not skip over parts. If several stretches tie for shortest, return the one that starts earliest along the belt. If no stretch can fill the kit, return an empty string.

The cell is fed at line rate, so your work must stay proportional to the combined length of belt and kit.

Examples

Example 1

Input
belt = "PQRSTQP", kit = "PQ"
Output
"PQ"

The first two parts already carry both stamps the kit asks for, and no single part can fill a two-stamp kit.

Example 2

Input
belt = "aabbcc", kit = "abcb"
Output
"abbc"

The kit lists b twice, so the stretch has to reach across both b parts as well as one a and one c.

Example 3

Input
belt = "abxba", kit = "ab"
Output
"ab"

Two stretches of length two can fill the kit, and the earlier of the two is the answer.

Example 4

Input
belt = "xy", kit = "z"
Output
""

No part on the belt carries the stamp z, so nothing can fill the kit.

Constraints

  • m == belt.length
  • n == kit.length
  • 1 <= m, n <= 10^5
  • belt and kit consist of uppercase and lowercase English letters

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def shortest_kit_stretch(belt: str, kit: str) -> str:
Java
public String shortestKitStretch(String belt, String kit)
September 7
Apply