Minimum Window Substring
Real-world version: Shortest Kit StretchThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def shortest_kit_stretch(belt: str, kit: str) -> str:public String shortestKitStretch(String belt, String kit)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Maximum Length Substring With Two OccurrencesReal-world version: Longest Twice-Only StretchEasy
- Substrings of Size Three with Distinct CharactersReal-world version: Three Clean Pigment WindowsEasy
- Longest Nice SubstringReal-world version: Longest Matched-Case RunEasy
- Longest Substring Without Repeating CharactersReal-world version: Largest Clean Tap BlockMedium