← DiffPush

Minimum Window Substring

DiffPush Tier Sliding Window · Hard Problems O(m + n) · O(n)

The Supply Chain Order Assembler

A fulfillment scanner reads one long stream of crate codes and must locate the shortest contiguous stretch that contains every part code on today's pick list — counting multiplicity, so a part demanded twice must appear twice. The scanner slides a frame over the stream, records the first complete covering stretch, then keeps tightening from the left as long as coverage holds, tracking the shortest frame ever seen.

Input: Two strings: s (the crate-code stream) and t (the pick list).

Output: Return the shortest substring of s covering every character of t with multiplicity, or an empty string if none exists.

Constraints

Examples

Example 1

Input: {"s":"ADOBECODEBANC","t":"ABC"}
Output: "BANC"
The tail "BANC" is the shortest stretch holding A, B and C simultaneously.

Example 2

Input: {"s":"a","t":"a"}
Output: "a"
The single character covers the demand exactly.

Solve this in your browser →

Also on LeetCode ↗