All problems
HardStrings

Minimum Window Substring

metaamazongooglemicrosoftlinkedinuberbloomberg

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.

Example 1:

Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.

Example 2:

Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.

Example 3:

Input: s = "a", t = "aa"
Output: ""
Explanation: Both 'a's from t must be included in the window. Since s only has one 'a', return empty string.

Examples

Example 1

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

Output: "BANC"

Explanation: The minimum window substring "BANC" (indices 9-12) contains all characters from t: 'A', 'B', and 'C'.

Example 2

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

Output: "a"

Explanation: The entire string s is the smallest window that contains all characters of t.

Constraints

  • -m == s.length
  • -n == t.length
  • -1 <= m, n <= 10^5
  • -s and t consist of uppercase and lowercase English letters.

Optimal Complexity

Time

O(m + n)

Space

O(m + n)

Practice this problem with an AI interviewer

TechInView conducts a full voice mock interview — the AI asks clarifying questions, evaluates your approach, watches you code, and scores you on 5 dimensions. Just like a real FAANG interview.

Start a free interview