Minimum Window Substring

Asked atGoldman SachsIntuitLinkedinMetaWWheelsEye
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

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 here

javascript

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+
  1. 01Count the required frequency of every character in t.
  2. 02Expand the right boundary and update how many required characters are still missing.
  3. 03Whenever the window is valid, repeatedly shrink from the left while recording the best window.
  4. 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.

Get 5x interviews10 free auto-applies. No card.
  • 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

Or practice this question first

Next question