滑动窗口问题

76. 最小覆盖子串

难度: 困难

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

注意:

  • 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
  • 如果 s 中存在这样的子串,我们保证它是唯一的答案。

示例 1:

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

示例 2:

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

 这个代码是理想型:

   public String minWindow(String s, String t) {
        char[] chars = s.toCharArray(), chart = t.toCharArray();
        int n = chars.length, m = chart.length;

        int[] hash = new int[128];
        for (char ch : chart) hash[ch]--;

        String res = "";
        for (int i = 0, j = 0, cnt = 0; i < n; i++) {
            hash[chars[i]]++;
            if (hash[chars[i]] <= 0) cnt++;
            while (cnt == m && hash[chars[j]] > 0) hash[chars[j++]]--;
            if (cnt == m)
                if (res.equals("") || res.length() > i - j + 1)
                    res = s.substring(j, i + 1);
        }
        return res;
    }

下面 这个代码是我写的


        public String minWindow(String s, String t) {

            if (s.length() < t.length()){
                return "";
            }

            int count = 0;
            int[] ASCII = new int[128];
            HashMap<Character, Integer> hashMap = new HashMap<>();
            char[] str = t.toCharArray();

            for (int i = 0; i < str.length; i++) {
                hashMap.put(str[i], hashMap.getOrDefault(str[i], 0) + 1);
            }
            int sums = hashMap.size();
            HashMap<Character, Integer> flag = new HashMap<>();
            char[] target = s.toCharArray();
            int flagI = 0;
            int min = Integer.MAX_VALUE;
            int flagStart = 0;
            int flagEnd = 0;
            int guo = 0;
            for (int i = 0; i < target.length; i++) {

                if (hashMap.containsKey(target[i])) {
                    if (flag.size() == 0) {
                        flagI = i;
                    }
                    flag.put(target[i], flag.getOrDefault(target[i], 0) + 1);

                    if (flag.get(target[i]) == hashMap.get(target[i])) {
                        count++;
                    }

                    if (sums == count) {
                        if (min > (i - flagI)) {
                            min = i - flagI;
                            flagStart = flagI;
                            flagEnd = i;
                            guo = 1;
                        }
                        for (int j = flagI; j <= i; j++) {
                            if (hashMap.containsKey(target[j])) {
                                if (flag.getOrDefault(target[j], 0) == 1) {
                                    flag.remove(target[j]);
                                    count--;
                                    flagI = j + 1;
                                    break;
                                } else {
                                    flag.put(target[j], flag.get(target[j]) - 1);
                                    if (flag.get(target[j]) < hashMap.get(target[j])) {
                                        count--;
                                        flagI = j + 1;
                                        break;
                                    }
                                }
                            }
                            flagI = j + 1;
                            if (i != flagI){
                                if (min > (i - flagI)) {
                                    min = i - flagI;
                                    flagStart = flagI;
                                    flagEnd = i;
                                }
                            }
                        }
                    }
                }
            }

            if (guo == 0){
                return "";
            }
            return s.substring(flagStart, flagEnd + 1);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值