最小覆盖子串

复制代码
public static String minWindow2(String s, String t){
    if (t.length()>s.length()){
        return "";
    }
    HashMap<Character, Integer> hashMap = new HashMap<>();
    for (Character i:t.toCharArray()){
        if (hashMap.containsKey(i)){
            hashMap.put(i,hashMap.get(i)-1);
        }else{
            hashMap.put(i,-1);
        }
    }
    //滑动窗口
    int start=0;
    int end=0;
    int max=Integer.MAX_VALUE;
    String res = "";
    while (end<s.length()){
        //不为null
        if (hashMap.get(s.charAt(end))!=null){
            hashMap.put(s.charAt(end),hashMap.get(s.charAt(end))+1);
            //判断符合不
            if (test18(hashMap)){
                //将start往后移动
                while (test18(hashMap)&&start<=end){
                    //更改max
                    if (end-start+1<max){
                        max = end-start+1;
                        res = s.substring(start,end+1);
                    }
                    if (hashMap.get(s.charAt(start))!=null){
                        //减去1
                        hashMap.put(s.charAt(start),hashMap.get(s.charAt(start))-1);
                    }
                    start++;
                }
                end++;
            }else{
                end++;
            }
        }else{
            end++;
        }
    }
    return res;
}
public static boolean test18(HashMap<Character, Integer> hashMap){
    for (Integer i:hashMap.values()){
        if (i<0){
            return false;
        }
    }
    return true;
}
相关推荐
我爱Jack12 分钟前
深入解析 LinkedList
java·开发语言
Owen_Q16 分钟前
Leetcode百题斩-二分搜索
算法·leetcode·职场和发展
矢志航天的阿洪37 分钟前
蒙特卡洛树搜索方法实践
算法
草莓熊Lotso1 小时前
【数据结构初阶】--顺序表(二)
c语言·数据结构·经验分享·其他
汤姆爱耗儿药1 小时前
数据结构——散列表
数据结构·散列表
UnderTheTime1 小时前
2025 XYD Summer Camp 7.10 筛法
算法
zstar-_1 小时前
Claude code在Windows上的配置流程
笔记·算法·leetcode
圆头猫爹1 小时前
第34次CCF-CSP认证第4题,货物调度
c++·算法·动态规划
27669582921 小时前
tiktok 弹幕 逆向分析
java·python·tiktok·tiktok弹幕·tiktok弹幕逆向分析·a-bogus·x-gnarly
秋说1 小时前
【PTA数据结构 | C语言版】出栈序列的合法性
c语言·数据结构·算法