src.minimum_window_substring¶
Classes
|
- class src.minimum_window_substring.Solution[source][source]¶
- minWindow(s: str, t: str) str [source]¶
Thought process¶
Problem uses some of the usual things, frequency map to store substring
t
and the window ofs
Key observation, to achieve an \(O(n)\) solutions we observe that we can define the condition of a substring being contained within a window as a single variable
have
being compared toneed
, where both variables are numeric counts representing the specific letters of the substringWe keep track of the window size and window start and end indices
Abstractly the problem uses the general sliding window approach, so the
r
pointer iterates through the loop as normal and thel
pointer iterates on a conditionDuring the iteration we consider 3 main conditions :
have != need
\(\rightarrow\) expand window ,window[c] == subs[c]
\(\rightarrow\) incrementhave
,while have == need
\(\rightarrow\) move thel
pointer and check if we found a new smallest substring
Notes¶
time complexity : \(O(n)\)
space complexity : \(O(n)\)