18. Minimum Window Substring
Last updated
Last updated
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
.
Algorithm
We start with two pointers, left and right initially pointing to the first element of the string S.
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.
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.
If the window is not desirable any more, we repeat step 2 onwards.
The above steps are repeated until we have looked at all the windows. The smallest window is returned
Time Complexity: O(S + T) Space Complexity: O(S + T)
.