Leetcode刷题76. 最小覆盖子串

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

 

示例 1:

输入:s = "ADOBECODEBANC", t = "ABC"
输出:"BANC"
示例 2:

输入:s = "a", t = "a"
输出:"a"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-window-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

感谢labuladong大神,传送门我写了套框架,把滑动窗口算法变成了默写题。

class Solution {
        public String minWindow(String s, String t) {
            if (s == null || s.length() == 0 || t == null || t.length() == 0) return "";
//            return minWindowI(s, t);
            return minWindowII(s, t);
        }

        //方法二:使用数组保存字符串
        //时间复杂度O(mn)
        private String minWindowII(String s, String t) {
            //字符本质上是ASCII码(纯英文字符),所以这里用的是该字符的ASCII码作为下标索引
            int[] need = new int[256];
            int[] window = new int[256];

            for (char c : t.toCharArray()) {
                need[c]++;
            }
            int left = 0, right = 0;
            //统计目前窗口中已经找到了多少个字符
            int valid = 0;
            //start为涵盖t中所有字符的子串在s中开始的位置,len为子串的长度
            int start = 0, len = Integer.MAX_VALUE;
            char[] str = s.toCharArray();
            while (right < str.length) {
                char c = str[right];
                //向右移动
                right++;
                window[c]++;

                //更新窗口数据
                //need[c]>0表示遇到t中字符,大于等于window[c]是满足t中字符出现的次数
                if (need[c] > 0 && need[c] >= window[c]) {
                    valid++;
                }

                //开始收缩窗口
                while (valid == t.length()) {
                    //在窗口收缩的时候更新left和len
                    if (right - left < len) {
                        start = left;
                        len = right - left;
                    }

                    char d = str[left];
                    //向左移动
                    left++;

                    //更新窗口数据
                    if (need[d] > 0 && need[d] >= window[d]) {
                        valid--;
                    }
                    window[d]--;
                }
            }
            return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
        }

        //方法一:使用HashMap定义两个窗口分别保存字符串t和字符串s中字符出现的次数
        //时间复杂度O(mn),n是s的长度
        //空间复杂度O(m),m为t的长度
        private String minWindowI(String s, String t) {
            Map need = new HashMap<>();
            Map window = new HashMap<>();

            for (char c : t.toCharArray()) {
                need.put(c, need.getOrDefault(c, 0) + 1);
            }
            int left = 0, right = 0;
            //当valid=need.size()时,需要收缩窗口
            int valid = 0;
            //start为涵盖t中所有字符的子串在s中开始的位置,len为子串的长度
            int start = 0, len = Integer.MAX_VALUE;
            char[] str = s.toCharArray();
            while (right < str.length) {
                char c = str[right];
                //向右移动
                right++;

                //更新窗口数据
                if (need.containsKey(c)) {
                    window.put(c, window.getOrDefault(c, 0) + 1);

                    //窗口中字符的个数满足字符串t中字符的个数
                    if (window.get(c).equals(need.get(c))) {
                        valid++;
                    }
                }

                //开始收缩窗口
                while (valid == need.size()) {
                    //在窗口收缩的时候更新left和len
                    if (right - left < len) {
                        start = left;
                        len = right - left;
                    }

                    char d = str[left];
                    //向左移动
                    left++;

                    //更新窗口数据
                    if (need.containsKey(d)) {
                        if (window.get(d).equals(need.get(d))) {
                            valid--;
                        }
                        window.put(d, window.get(d) - 1);
                    }
                }
            }
            return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
        }
    }

 

你可能感兴趣的:(字符串,哈希表,字符串,Sliding,Window)