Minimum Window Substring
The problem
Given two strings s and t. Find the smallest window substring of s that includes all characters in t (including duplicates) , in the window. Return the empty string "" if no such substring exists.
Input : s = "ADOBECODEBANC" , t = "ABC" Output : "BANC" Explanation : The minimum window substring of string s that contains the string t is "BANC".
Input : s = "a" , t = "a" Output : "a" Explanation : The complete string is the minimum window
Input : s = "aAbBDdcC" , t = "Bc"
- 1 <= n , m <= 105
- n = s.length
- m = t.length
- string s and t consist of uppercase and lowercase letters.
cpp
class Solution {
public:
string minWindow(string s, string t) {
//your code goes here
}
};java
class Solution {
public String minWindow(String s, String t) {
//your code goes here
}
}python
class Solution:
def minWindow(self, s: str, t: str) -> str:
#your code goes herejavascript
class Solution {
minWindow(s, t) {
//your code goes here
}
}csharp
public class Solution {
public string MinWindow(string s, string t) {
}
}go
func minWindow(s string, t string) string {
}Stuck? Show a way to structure it+
- 01Count the required frequency of every character in t.
- 02Expand the right boundary and update how many required characters are still missing.
- 03Whenever the window is valid, repeatedly shrink from the left while recording the best window.
- 04Return the best recorded slice, or an empty string if no valid window was found.
Reference answer
Then expect these follow-ups
How would you adapt the solution for arbitrary Unicode characters?
Tests: representation choices
What invariant tells you that the current window is valid?
Tests: correctness reasoning
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question