class Solution {
public String minWindow(String s, String t) {
int[] map = new int[128];
for(char c:t.toCharArray()){
map[c]++;
}
int start = 0;
int end = 0;
int minStart = 0;
int minLen = Integer.MAX_VALUE;
int counter = t.length();
while(end < s.length()){
char c = s.charAt(end);
if(map[c]>0)
counter--;
map[c]--;
end++;
while(counter == 0){
if(end - start < minLen){
minLen = end - start;
minStart = start;
}
if(map[s.charAt(start)] == 0){ //make it invalid,跳出loop
counter++;
//map里值<0的就是不在t中,或者是重复的
map[s.charAt(start)]++;
start++; //去掉重复 和 不在t中的 ,也就是窗口的left向右移
}
else{
map[s.charAt(start)]++;
start++; //去掉重复 和 不在t中的 ,也就是窗口的left向右移
}
}
}
return (minLen == Integer.MAX_VALUE)? "" : s.substring(minStart, minStart + minLen);
}
}
[leetcode] 76. Minimum Window Substring
最新推荐文章于 2020-08-17 07:22:22 发布