Leecode76 minimum-window-substring

Title description

Given two character strings S and T, it is required to find the shortest substring containing all the characters in T in S within the time complexity of O (n).
For example:
S = "ADOBECODEBANC"
T = "ABC"
The shortest substring found is "BANC".
Note:
If there is no substring in S that contains all the characters in T, the empty string "" is returned; the substring that
meets the condition There may be many, but the problem guarantees that the shortest substring satisfying the condition is unique.

java code

public class Solution {
    int tArr[] = new int[256];
    public String minWindow(String S, String T) {
        if(S==null||T == null || S.length() == 0 || T.length()==0){
            return "";
        }
        int matchCount = 0;
        int count = 0;
        String res = "";
        //tArr初始化之后不改变        
        for(char ch : T.toCharArray()){
            //统计 T中各字符数量
            tArr[ch]++;
            count++;
        }
        int sArr[] = new int [256];
        int left = 0;
        int right = 0;
        //从某一下标开始(包括),在S中找到下一个符合T的字符下标。
        left = findNextT(0,S);
        //没有符合T的字符
        if(left == S.length()){
            return "";
        }
        right = left;
        while(right < S.length()){
            //如果没加满           
            if(sArr[S.charAt(right)] < tArr[S.charAt(right)]){
                matchCount++;
            }
            //可能会匹配了多余的
             sArr[S.charAt(right)]++;
            //符合条件的
            while(right < S.length() && matchCount == count){
                //更新最小
                if(res == "" || res.length() > right - left +1){
                    res = S.substring(left,right+1);
                }
                //left右移  只有不满了 才要减小匹配数
                if(sArr[S.charAt(left)] <= tArr[S.charAt(left)]){
                    matchCount --;
                }
                sArr[S.charAt(left)]--;
                left = findNextT(left+1,S);                
            }
          right = findNextT(right+1,S);         
        }
        return res;
    }
    public int findNextT(int start,String s){
        if(start >= s.length()){
            return s.length();
        }
        while(start < s.length()){
            //tArr 里面有此数
            if(tArr[s.charAt(start)] != 0){
                return start;
            }
            start ++;
        }
        return start;
    }
}
Published 72 original articles · liked 0 · visits 724

Guess you like

Origin blog.csdn.net/weixin_40300702/article/details/105441491