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.
这道题的要求是在给定的字符串S中找到最小的窗口使其完全包含字符串T中所有字符,如果不存在,则返回空串”“。
只记思路吧,动态规划。
用一个哈希表记录目标字符串每个字母的个数,一个哈希表记录窗口中每个字母的个数。先找到第一个有效的窗口,用两个指针标出它的上界和下界。然后每次窗口右界向右移时,将左边尽可能的右缩,右缩的条件是窗口中字母的个数不小于目标字符串中字母的个数。
tips:只有字母的情况下(包括大小写),可以用一个128位的char数组来表示哈希表,因为字母的ASCII码都在这个范围内
(a~z:97~122;A~Z:65~90)