Minimum Window Substring
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).
(包含字符串的最小窗口)
Note:
- If there is no such window in S that covers all characters in T, return the empty string “”.
- If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
Example:
1. 哈希表 & 双指针
很直观,这题需要用到 HashMap 来存储目标字符串。用 left,right 两个指针指向 S,在遍历 S 的过程中,right 不断地右移来找到复合要求的字符串,也就是 required_char == 0 的时候,考虑向右移动 left,直到发现了当前的 S[left: right+1] 不符合要求的时候,重新向右移动 right。具体实现过程如下:
1 | import collections |