Minimum Window Substring - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
💡 Note: The minimum window substring "BANC" includes one 'A', one 'B', and one 'C' from t.
Example 2 — No Valid Window
$ Input: s = "a", t = "aa"
Output: ""
💡 Note: String s only has one 'a', but t requires two 'a's. No valid window exists.
Example 3 — Entire String
$ Input: s = "a", t = "a"
Output: "a"
💡 Note: The minimum window is the entire string s, which is "a".

Constraints

  • 1 ≤ s.length, t.length ≤ 105
  • s and t consist of uppercase and lowercase English letters

Visualization

Tap to expand
Minimum Window Substring ProblemInput:s = "ADOBECODEBANC"t = "ABC"Need: A(1), B(1), C(1)Process:ADOBEC (len=6)CODEBA (len=6)BANC (len=4)Output:"BANC"✓ Contains A, B, C with minimum lengthSliding window technique finds optimal substring efficiently
Understanding the Visualization
1
Input
Source string s and target string t with required characters
2
Process
Find minimum window in s containing all characters from t
3
Output
Return shortest valid substring or empty string
Key Takeaway
🎯 Key Insight: Use sliding window with hash maps to efficiently track character frequencies while maintaining minimum valid window
Asked in
Facebook 85 Amazon 72 Microsoft 68 Google 65 Apple 45
336.8K Views
Very High Frequency
~25 min Avg. Time
8.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen