2、algorithm滑动窗口技巧:76. 最小覆盖子串[困难]、438. 找到字符串中所有字母异位词[中等]

本系列文章将跟着funking-algorithm刷题
Github原版地址:https://github.com/labuladong/fucking-algorithm

76. 最小覆盖子串[困难]

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

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

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

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

提示:

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

进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗?

My Answer

	/**
     * 76. 最小覆盖子串
     *
     * @param s
     * @param t
     * @return
     */
    public String minWindow(String s, String t) {
        HashMap<Character, Integer> window = new HashMap();
        HashMap<Character, Integer> need = new HashMap();
        char[] chars = t.toCharArray();
        // 初始化 need 字符
        for (char ch : chars) {
            int num = need.get(ch) == null ? 0 : need.get(ch);
            need.put(ch, num + 1);
            window.put(ch, 0);
        }
        
        int left = 0, right = 0;
        int start = 0, miniLength = Integer.MAX_VALUE;
        int match = 0;
        chars = s.toCharArray();
        while (right < s.length()) {
            char ch = chars[right];
            right++;

            // 含有字符 ch
            if (need.get(ch) != null) {
                // 包含字符 +1 ,只有一次刚好相等
                window.put(ch, window.get(ch) + 1);
                if (window.get(ch).equals(need.get(ch))) {
                    match++;
                }
            }

            while (match == need.size()) {
                // 匹配
                if (miniLength > right - left) {
                    miniLength = right - left;
                    start = left;
                }
                ch = chars[left];
                if (need.get(ch) != null) {
                    window.put(ch, window.get(ch) - 1);
                    if (need.get(ch).equals(window.get(ch) + 1)) {
                        match--;
                    }
                }
                left++;
            }
        }
        return miniLength == Integer.MAX_VALUE ? "" : s.substring(start, start + miniLength);
    } 

438. 找到字符串中所有字母异位词给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。

字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。

说明:

字母异位词指字母相同,但排列不同的字符串。
不考虑答案输出的顺序。
示例 1:

输入:
s: "cbaebabacd" p: "abc"

输出:
[0, 6]

解释:
起始索引等于 0 的子串是 "cba", 它是 "abc" 的字母异位词。
起始索引等于 6 的子串是 "bac", 它是 "abc" 的字母异位词。

示例 2:

输入:
s: "abab" p: "ab"

输出:
[0, 1, 2]

解释:
起始索引等于 0 的子串是 "ab", 它是 "ab" 的字母异位词。
起始索引等于 1 的子串是 "ba", 它是 "ab" 的字母异位词。
起始索引等于 2 的子串是 "ab", 它是 "ab" 的字母异位词。

My Answer

将上述答案稍加修改即可

	/**
     * 438. 找到字符串中所有字母异位词
     *
     * @param s
     * @param p
     * @return
     */
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        HashMap<Character, Integer> window = new HashMap();
        HashMap<Character, Integer> need = new HashMap();
        char[] chars = p.toCharArray();
        // 初始化 need 字符
        for (char ch : chars) {
            int num = need.get(ch) == null ? 0 : need.get(ch);
            need.put(ch, num + 1);
            window.put(ch, 0);
        }

        int left = 0, right = 0;
        int match = 0;
        chars = s.toCharArray();
        while (right < s.length()) {
            char ch = chars[right];
            right++;

            // 含有字符 ch
            if (need.get(ch) != null) {
                // 包含字符 +1 ,只有一次刚好相等
                window.put(ch, window.get(ch) + 1);
                if (window.get(ch).equals(need.get(ch))) {
                    match++;
                }
            }

            while (match == need.size()) {
                // 匹配
                if (right - left == p.length()) {
                    list.add(left);
                }
                ch = chars[left];
                if (need.get(ch) != null) {
                    window.put(ch, window.get(ch) - 1);
                    if (need.get(ch).equals(window.get(ch) + 1)) {
                        match--;
                    }
                }
                left++;
            }
        }
        return list;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值