Skip to content
BytePatterns

Minimum Window Cover

HardStrings#sliding-window#hash-map~45m

Problem

Given a text and a set of required characters, find the shortest contiguous stretch of the text that contains every required character, counting duplicates. A requirement that lists the same character twice needs two copies inside the stretch. Return the empty text when no stretch qualifies, and the earliest one when several tie for shortest.

Examples

Input:  text = "ADOBECODEBANC", need = "ABC"
Output: "BANC"
Why:    shorter than ADOBEC, which also covers the requirement
Input:  text = "aa", need = "aa"
Output: "aa"
Why:    duplicates in the requirement must each be matched
Input:  text = "a", need = "aa"
Output: ""
Why:    edge case, the text cannot supply a second copy

Hints

0 / 3

Stuck on the idea rather than the code? Sliding Window covers it.