76. 最小覆盖子串

76. 最小覆盖子串

滑动窗口

经典写法

java 复制代码
class Solution {
    public String minWindow(String s, String t) {
        HashMap<Character, Integer> window = new HashMap<>(), need = new HashMap<>();
        for(char c : t.toCharArray()) need.merge(c, 1, Integer::sum);
        int cnt = 0, start = 0, len = Integer.MAX_VALUE;

        for(int l = 0, r = 0; r < s.length(); r++){
            // 窗口增大的逻辑
            char c = s.charAt(r);
            if(need.containsKey(c)){
                window.merge(c, 1, Integer::sum);
                if(need.get(c).equals(window.get(c))) cnt++;
            }

            while(cnt == need.size()){  // 窗口何时缩小
                // 更新答案
                if(len > r - l + 1){
                    len = r - l + 1;
                    start = l;
                }
                // 窗口缩小的逻辑
                c = s.charAt(l++);
                if(need.containsKey(c)){
                    if(need.get(c).equals(window.get(c))) cnt--;
                    window.merge(c, -1, Integer::sum);
                }
            }
        }

        return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
    }
}

简洁写法

java 复制代码
class Solution {
    public String minWindow(String s, String t) {
        char[] chs = s.toCharArray(), cht = t.toCharArray();
        int[] hash = new int[128];
        for(char c : cht) hash[c]--;
        int cnt = 0, m = cht.length;
        String res = "";

        for(int l = 0, r = 0; r < chs.length; r++){
            hash[chs[r]]++;
            if(hash[chs[r]] <= 0) cnt++;

            while(cnt == m && hash[chs[l]] > 0) hash[chs[l++]]--;
            
            if(cnt == m){
                if(res.equals("") || res.length() > r - l + 1){
                    res = s.substring(l, r + 1);
                }
            }
        }

        return res;
    }
}

一个坑

两个 Integer 相等比较应该用 equals,而不是 ==。

一个 Integer 和一个 int 比较,Integer 会自动拆箱,可以用 ==。

java 复制代码
class Solution {
    public String minWindow(String s, String t) {
        int i = 0;
        HashMap<Character, Integer> mapS = new HashMap<>();
        HashMap<Character, Integer> mapT = new HashMap<>();
        for(char c : t.toCharArray()){
            //mapT.merge(c, 1, Integer::sum);
            mapT.put(c, mapT.getOrDefault(c, 0) + 1);
        }
        int cnt = 0;
        int res = Integer.MAX_VALUE, left = 0, right = 0;

        for(int j = 0; j < s.length(); j++){
            char c = s.charAt(j);
            if(mapT.containsKey(c)){
                //mapS.merge(c, 1, Integer::sum);
                mapS.put(c, mapS.getOrDefault(c, 0) + 1);
                if(mapS.get(c).equals(mapT.get(c))){  // 一个坑:Integer 相等比较应该用 equals,而不是 ==
                    cnt++;
                }
            }
            
            while(cnt == mapT.size()){
                if(res > j - i + 1){
                    left = i;
                    right = j;
                    res = j - i + 1;
                }
                c = s.charAt(i++);
                if(mapT.containsKey(c)){
                    //mapS.merge(c, -1, Integer::sum);
                    mapS.put(c, mapS.getOrDefault(c, 0) - 1);
                    if(mapS.get(c).compareTo(mapT.get(c)) < 0){
                        cnt--;
                    }
                }
            }

        }

        return res == Integer.MAX_VALUE ? "" : s.substring(left, right + 1);
    }
}
相关推荐
GreatSQL社区2 小时前
主从复制SQL线程中断——字符集砸出的坑
java·数据库·算法
Tisfy3 小时前
LeetCode 2333.最小差值平方和:排序 + 数学计算(不用二分)
数学·算法·leetcode·题解·贪心·排序
aqiu1111113 小时前
【C++算法打怪专栏】LeetCode 217. 存在重复元素(解法总结)
c++·算法·leetcode
yi0114 小时前
DAY28: LeetCode 111. 二叉树的最小深度
人工智能·笔记·python·算法·leetcode·二叉树·递归
wuyk5554 小时前
27.LeetCode 2265. 统计值等于子树平均值的节点数 —— 后序遍历实战
stm32·单片机·嵌入式硬件·算法
xiangyun614 小时前
C++ list 模拟实现(二):const迭代器与反向迭代器,模板的黑魔法
开发语言·数据库·c++·算法·stl·list
xxwl5855 小时前
三、区域赛算法模版第三章---字符串相关算法
笔记·学习·算法
信奥卷王5 小时前
[GESP202509 六级] 划分字符串(含视频)
数据结构·算法
酒醉的胡铁5 小时前
相似度搜索的常见算法
算法·ai
局部有解5 小时前
Leetcode Hot100 - 最长连续序列
算法·leetcode·职场和发展