给出两个字符串 s 和 t,要求在 s 中找出最短的包含 t 中所有字符的连续子串。

image.png
思路:
双指针方法 l,r两个指针
1、先移动r,判断r-l字符串中是否包含T,如果包含记录字符串其实位置、终点位置和长度
2、包含计算后,可以移动l,移动到count>0,
import java.util.*;
public class Solution {
    /**
     * 
     * @param S string字符串 
     * @param T string字符串 
     * @return string字符串
     */
    public String minWindow (String S, String T) {
         // write code here
        int [] map = new int[128];
       // 初始化T 字符,在map对应位置++
        for(int i = 0;i<T.length();i++){
            int  s = T.charAt(i);
            map[T.charAt(i)]++;
        }
        int l = 0;
        int r = 0;
        int length = Integer.MAX_VALUE;
        int count = T.length();
        int head = 0;
        while(r<S.length()){
            if(map[S.charAt(r++)]-- >0){
                count --;
            }
            while(count == 0){
                if(r-l<length){
                    head = l;
                    length = r-l;
                }
                //如果map >= 0 说明匹配过,未匹配应该负数
                if(map[S.charAt(l++)]++ >= 0){
                    count ++;
                }
            }
        }
        if(length == Integer.MAX_VALUE){
            return "";
        }
        return S.substring(head,head+length);
    }
}