AlgoLounge

Minimum Window Substring

hard

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 "".

The testcases will be generated such that the answer is unique.

Examples

Example 1

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

The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.

Example 2

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

The entire string s is the minimum window.

Example 3

Input:  s = "a", t = "aa"
Output: ""

Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.

Constraints

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 10⁵
  • s and t consist of uppercase and lowercase English letters.
TypeScript
Loading editor…
List of test cases (10)
Case 1
s = "ADOBECODEBANC"
t = "ABC"
Case 2
s = "a"
t = "a"
Case 3
s = "a"
t = "aa"
Case 4
s = "aa"
t = "aa"
Case 5
s = "abc"
t = "b"
Case 6
s = "ab"
t = "b"
Case 7
s = "bba"
t = "ab"
Case 8
s = "cabwefgewcwaefgcf"
t = "cae"
Case 9
s = "abc"
t = "ac"
Case 10
s = "abc"
t = "d"