Skip to main content

Minimum Window Substring

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

Problems20 / 152
Sign in

Minimum Window Substring

HARDStringSliding Window

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

Example 1

Input:  s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"

Example 2

Input:  s = "a", t = "a"
Output: "a"
Show hint

Use sliding window with two pointers. Expand right until all characters are found, then shrink left to minimize window while maintaining all characters.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
s =
"ADOBECODEBANC"
t =
"ABC"
Expected
"BANC"
Run with your own input (JSON)