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
Hiring now
Linkedin has 196 open roles right now
landed applies to roles like these for you, on each company’s own site, with answers written for the role.
Software Engineer - Systems and Infrastructure
Mountain View, CA, United States
Staff Software Engineer
Mountain View, CA, United States
Staff Software Engineer, Systems Infrastructure
Bengaluru, KA, India
Next question