推荐一个作者详解,感觉很神奇......
作者:Mcdull0921
链接:https://leetcode-cn.com/problems/minimum-window-substring/solution/tong-su-qie-xiang-xi-de-miao-shu-hua-dong-chuang-k/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
个人微改:
class window{
public String minWindow(String s,String t) {
if(s==null||t==null||s.length()==0||t.length()==0) {
return "";
}
int[] need = new int[128];
int size = Integer.MAX_VALUE;
int left=0,right=0,start=0,count=t.length();
for(int i=0;i<t.length();i++) {
need[t.charAt(i)]++;
}
while(right<s.length()) {
char c = s.charAt(right);
if(need[c]>0) {
count--;
}
need[c]--;
if(count==0) {
while(left<right && need[s.charAt(left)]<0) {
need[s.charAt(left)]++;
left++;
}
if(right-left+1<size) {
size = right-left+1;
start = left;
}
need[s.charAt(left)]++;
left++;
count++;
}
right++;
}
return size==Integer.MAX_VALUE ? "":s.substring(start,start+size);
}
}
public class MinimumWindowSubstring {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "ADOBECODEBANC";
String t = "ABC";
window hah = new window();
System.out.println(hah.minWindow(s, t));
}
}