您的位置:首页 > 其它

76. Minimum Window Substring

2017-08-09 00:00 197 查看
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 empty string
""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

思路

用一个哈希表记录目标字符串每个字母的个数,一个哈希表记录窗口中每个字母的个数。先找到第一个有效的窗口,用两个指针标出它的上界和下界。然后每次窗口右界向右移时,将左边尽可能的右缩,右缩的条件是窗口中字母的个数不小于目标字符串中字母的个数。

注意

用一个数组来保存每个字符出现的次数,比哈希表容易

保存结果子串的起始点初值为-1,方便最后判断是否有正确结果

public String minWindow(String S, String T) {
int[] srcHash = new int[255];
// 记录目标字符串每个字母出现次数
for(int i = 0; i < T.length(); i++){
srcHash[T.charAt(i)]++;
}
int start = 0,i= 0;
// 用于记录窗口内每个字母出现次数
int[] destHash = new int[255];
int found = 0;
int begin = -1, end = S.length(), minLength = S.length();
for(start = i = 0; i < S.length(); i++){
// 每来一个字符给它的出现次数加1
destHash[S.charAt(i)]++;
// 如果加1后这个字符的数量不超过目标串中该字符的数量,则找到了一个匹配字符
if(destHash[S.charAt(i)] <= srcHash[S.charAt(i)]) found++;
// 如果找到的匹配字符数等于目标串长度,说明找到了一个符合要求的子串
if(found == T.length()){
// 将开头没用的都跳过,没用是指该字符出现次数超过了目标串中出现的次数,并把它们出现次数都减1
while(start < i && destHash[S.charAt(start)] > srcHash[S.charAt(start)]){
destHash[S.charAt(start)]--;
start++;
}
// 这时候start指向该子串开头的字母,判断该子串长度
if(i - start < minLength){
minLength = i - start;
begin = start;
end = i;
}
// 把开头的这个匹配字符跳过,并将匹配字符数减1
destHash[S.charAt(start)]--;
found--;
// 子串起始位置加1,我们开始看下一个子串了
start++;
}
}
// 如果begin没有修改过,返回空
return begin == -1 ? "" : S.substring(begin,end + 1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: