其他分享
首页 > 其他分享> > LeetCode76 最小覆盖子串

LeetCode76 最小覆盖子串

作者:互联网

滑动窗口+map

设定左右双指针,若滑动窗口内不包含t中所有的字符则右指针右移,若包含t所有字符则左指针右移同时更新最小值和对应边界直到不包含所有的,以此循环直到s的最后

class Solution {
    Map<Character,Integer> need = new HashMap<>();
    Map<Character,Integer> have = new HashMap<>();
    public String minWindow(String s, String t) {
        int sLen = s.length(),tLen = t.length();
        for(int i=0;i<tLen;i++){
            char c = t.charAt(i);
            need.put(c,need.getOrDefault(c,0)+1);
        }
        int l = 0,r = 0,length = 0;
        int ansL = -1,ansR = -1,min = Integer.MAX_VALUE;
        while(r<sLen){
            char cur = s.charAt(r);
            have.put(cur,have.getOrDefault(cur,0)+1);
            r++;
            while (check()&&l<r){
                if(min>r-l+1){
                    min = r-l+1;
                    ansL = l;
                    ansR = r;
                }
                char cl = s.charAt(l);
                have.put(cl,have.getOrDefault(cl,0)-1);
                l++;
            }
        }
        return ansL==-1?"":s.substring(ansL,ansR);
    }
    private boolean check(){
        for(char c:need.keySet()){
            int count = have.getOrDefault(c,0);
            if(count<need.get(c)){
                return false;
            }
        }
        return true;
    }
}

滑动窗口+数组

滑动窗口采用map保存字符和个数,可用数组代替map优化空间复杂度

class Solution {
    public String minWindow(String s, String t) {
        int sLen = s.length(),tLen = t.length();
        int[] need = new int[128] , have = new int[128];
        int l = 0,r = 0,length = 0;
        int ansL = -1,ansR = -1,min = Integer.MAX_VALUE;
        for(int i=0;i<tLen;i++){
            need[t.charAt(i)]++;
        }
        while(r<sLen){
            char c = s.charAt(r);
            have[c]++;
            r++;
            if(have[c]<=need[c]){
                length++;
            }
            while (length==tLen&&l<r){
                if(min>r-l+1){
                    min = r-l+1;
                    ansL = l;
                    ansR = r;
                }
                char cl = s.charAt(l);
                have[cl]--;
                l++;
                if(have[cl]<need[cl]){
                    length--;
                }
            }
        }
        return ansL==-1?"":s.substring(ansL,ansR);
    }
}

标签:子串,LeetCode76,String,cl,int,复杂度,最小,length,ansL
来源: https://www.cnblogs.com/ermiao-zy/p/15770613.html