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);
    }
}
相关推荐
Tiandaren4 分钟前
Selenium 4 教程:自动化 WebDriver 管理与 Cookie 提取 || 用于解决chromedriver版本不匹配问题
selenium·测试工具·算法·自动化
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
秋说3 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy3 小时前
力扣61.旋转链表
算法·leetcode·链表
卡卡卡卡罗特5 小时前
每日mysql
数据结构·算法
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
lifallen7 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
lixzest7 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
EndingCoder7 小时前
搜索算法在前端的实践
前端·算法·性能优化·状态模式·搜索算法