题目描述
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.
def minimumSubstring(S, T): map = {} for i in T: if i not in map: map[i] = 1 else: map[i] += 1 res = '' begin = 0 d = len(S) end = 0 count = len(T) while end < len(S): if S[end] not in map: map[S[end]] = -1 if map[S[end]] > 0 : map[S[end]] -= 1 count -= 1 while count == 0: if end - begin < d: d = end - begin + 1 res = S[begin:begin+d] if S[begin] in map and map[S[begin]] == 0: begin += 1 count += 1 else: begin += 1 end += 1 return res print minimumSubstring('bcdaddaaca', 'addcab')
寻找最小覆盖子串
本文介绍了一种在字符串S中找到包含字符串T所有字符的最短子串的方法,并提供了一个具体的实现示例。该算法复杂度为O(n),适用于解决实际问题中的子串查找需求。
798

被折叠的 条评论
为什么被折叠?



