You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note: If there is no such window in S that covers all characters in T, return the emtpy string "". If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
'''
from __future__ importdivision
importrandom
# O(|S|log|T|)
defmin_window_substr(S, T):
ifS==''orT=='': return''
last_seen= {}
start=0; end=len(S)-1
T=set(T)
# find such a substring ended at i-th character.
fori, chinenumerate(S):
ifchnotinT: continue
last_seen[ch] =i
iflen(last_seen) ==len(T):
# all chars have been seen
first=min(last_seen.values()) #**We can use a priority queue, O(logn)