Day41 最小覆盖子串

作者: Shimmer_ | 来源:发表于2021-03-07 22:31 被阅读0次

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

    https://leetcode-cn.com/problems/minimum-window-substring/

    注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

    示例1:

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

    示例2:

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

    提示:

    1 <= s.length, t.length <= 105
    s 和 t 由英文字母组成

    Java解法

    思路:

    • 初步想法:滑动数组来处理,在找到匹配字符时数据开始匹配,匹配到全部时,开始移动数组,在移动过程中记录最短字符匹配串,但考虑到重复字符匹配没有很好的记录方式暂时卡住
    • 参考官方解,使用hashmap存储char及个数,不用记录位置,只做当前窗口是否符合要求的判断
    package sj.shimmer.algorithm.m2;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Created by SJ on 2021/3/6.
     */
    
    
    class D41 {
        public static void main(String[] args) {
    //        System.out.println(minWindow("ADOBECODEBANC", "ABC"));
            System.out.println(minWindow("a", "a"));
        }
    
        static Map<Character, Integer> tCharNum = new HashMap<>();
        static Map<Character, Integer> mCharNum = new HashMap<>();
    
        public static String minWindow(String s, String t) {
    
            String matchS = "";
    
            if (s == null || s.equals("") || t == null || t.equals("")) {
                return matchS;
            }
            int tLength = t.length();
            int sLength = s.length();
            for (int i = 0; i < tLength; i++) {
                char key = t.charAt(i);
                tCharNum.put(key, tCharNum.getOrDefault(key, 0) + 1);
            }
            int resultLeft = -1;
            int resultRight = -1;
            int resultLength = Integer.MAX_VALUE;
            int left = 0;
            int right = -1;
            while (right < sLength) {
                right++;
                if (right < sLength && tCharNum.containsKey(s.charAt(right))) {
                    mCharNum.put(s.charAt(right), mCharNum.getOrDefault(s.charAt(right), 0) + 1);
                }
                while (left <= right && checkIn()) {
                    if (right - left + 1 < resultLength) {//记录最短长度
                        resultLength = right - left + 1;
                        resultLeft = left;
                        resultRight = left + resultLength;//拼接时需要增加一位
                    }
                    if (tCharNum.containsKey(s.charAt(left))) {
                        mCharNum.put(s.charAt(left), mCharNum.getOrDefault(s.charAt(left), 0) - 1);
                    }
                    left++;
                }
            }
            matchS = resultLeft == -1 ? "" : s.substring(resultLeft, resultRight);
            return matchS;
        }
    
        private static boolean checkIn() {
            for (Map.Entry<Character, Integer> entry : tCharNum.entrySet()) {
                if (entry.getValue() > mCharNum.getOrDefault(entry.getKey(), 0)) {
                    return false;
                }
            }
            return true;
        }
    }
    
    image

    官方解

    https://leetcode-cn.com/problems/minimum-window-substring/solution/zui-xiao-fu-gai-zi-chuan-by-leetcode-solution/

    1. 滑动窗口

      我的参考解法

      • 时间复杂度:O(C⋅∣s∣+∣t∣)

      • 空间复杂度: O(C) ,设字符集大小为 C

    相关文章

      网友评论

        本文标题:Day41 最小覆盖子串

        本文链接:https://www.haomeiwen.com/subject/adluqltx.html