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);
    }
}
相关推荐
zxsz_com_cn9 分钟前
设备预测性维护算法核心功能有哪些?六大模块拆解智能运维的“技术骨架”
运维·算法
期末考复习中,蓝桥杯都没时间学了11 分钟前
力扣刷题13
数据结构·算法·leetcode
2201_7569890921 分钟前
C++中的事件驱动编程
开发语言·c++·算法
多米Domi01132 分钟前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
2301_8223776533 分钟前
模板元编程调试方法
开发语言·c++·算法
故以往之不谏1 小时前
函数--值传递
开发语言·数据结构·c++·算法·学习方法
渐暖°1 小时前
【leetcode算法从入门到精通】5. 最长回文子串
vscode·算法·leetcode
今天_也很困1 小时前
LeetCode热题100-560. 和为 K 的子数组
java·算法·leetcode
v_for_van1 小时前
力扣刷题记录2(无算法背景,纯C语言)
c语言·算法·leetcode
2301_811232981 小时前
低延迟系统C++优化
开发语言·c++·算法