filmov
tv
Minimum Window Substring: Utilizing Two Pointers & Tracking Character Mappings With A Hashtable

Показать описание
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given a string S and a string T, find the minimum window in S which will contain all the characters in T (matching or exceeding in frequency) in complexity O(n).
Anytime we have time reduced to linear time and we know that much must be done, that is indicative of using some sort of auxiliary structure to keep track of information for us so time can stay low in complexity.
Complexities
s = length of search string
t = length of "pattern" or "character" string
Time: O( s + t )
At worst we will touch each element twice, once by the left pointer and once by the right pointer.
Ex:
s = "aaaaat"
t = "t"
We will spend t time to build the character requirements hashtable.
Space: O( s + t )
At worst the window hashtable will have a mapping for every character in s.
At worst t will have all unique characters.
s = "abcdef"
t = "abcdef"
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is 13.7 in the fantastic book Elements of Programming Interviews
Комментарии