Minimum Window Substring

Jaydeep
2 min readMay 1, 2021

Given two strings s and t, return the minimum window in s which will contain all the characters in t. If there is no such window in s that covers all characters in t, return the empty string "".

Note that If there is such a window, it is guaranteed that there will always be only one unique minimum window in s.

Example 1:

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

Example 2:

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

Constraints:

  • 1 <= s.length, t.length <= 105
  • s and t consist of English letters.

Sliding Window

Intuition

The question asks us to return the minimum window from the string S which has all the characters of the string T. Let us call a window desirable if it has all the characters from T.

We can use a simple sliding window approach to solve this problem.

In any sliding window-based problem we have two pointers. One right pointer whose job is to expand the current window and then we have the left pointer whose job is to contract a given window. At any point in time, only one of these pointers move and the other one remains fixed.

The solution is pretty intuitive. We keep expanding the window by moving the right pointer. When the window has all the desired characters, we contract (if possible) and save the smallest window till now.

The answer is the smallest desirable window.

For eg. S = "ABAACBAB" T = "ABC". Then our answer window is "ACB" and shown below is one of the possible desirable windows.

Algorithm

  1. We start with two pointers, left and right initially pointing to the first element of the string S.
  2. We use the right pointer to expand the window until we get a desirable window i.e. a window that contains all of the characters of T.
  3. Once we have a window with all the characters, we can move the left pointer ahead one by one. If the window is still a desirable one we keep on updating the minimum window size.
  4. If the window is not desirable anymore, we repeat step2 onwards.

The above steps are repeated until we have looked at all the windows. The smallest window is returned.

--

--

Jaydeep

Full Stack Programmer, love to solve problem’s during free time.