Skip to content
CodeShare
All problems

Minimum Window Substring

Find the shortest stretch of text containing every required letter.

HardStringsSliding windowHash map

Given strings s and t, return the shortest substring of s that contains every character of t, including duplicates. If no such substring exists, return an empty string.

When an answer exists, it is guaranteed to be unique.

Examples

Example 1
Input
s = "ADOBECODEBANC", t = "ABC"
Output
"BANC"
Example 2
Input
s = "a", t = "a"
Output
"a"
Example 3
Input
s = "a", t = "aa"
Output
""
Why
t needs two a's, but s only has one.

Constraints

  • 1 <= s.length, t.length <= 10^5
  • s and t contain uppercase and lowercase English letters.