Minimum Window Substring

Hard Sliding Window AI round

s and t are strings of English letters.

A window is a stretch of s, taken without gaps, that holds every character of t as many times as t has it. An upper case letter and its lower case are different characters. The tests are made so that, when s has a window, exactly one window is the shortest.

Return the shortest window, or the empty string "" when s has none.

Example 1

Input: s = "aPxkzPPyQ", t = "PPk"
Output: "kzPP"

t needs P twice and k once. From the k to the second P after it is 4 characters; every other stretch with two P's and a k is longer.

Example 2

Input: s = "kayakSNOWhat", t = "tSk"
Output: "kSNOWhat"

Example 3

Input: s = "HelloWorld", t = "hw"
Output: ""

Constraints

  • 1 ≤ s.length, t.length ≤ 10⁵
  • s and t hold only English letters, upper and lower case
  • When s has a window, exactly one window is the shortest

Watch the explanation

More Sliding Window

All Sliding Window problems