前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >76. 最小覆盖子串

76. 最小覆盖子串

作者头像
张伦聪zhangluncong
发布2022-10-26 18:09:50
3340
发布2022-10-26 18:09:50
举报

给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串。

示例:

代码语言:javascript
复制
输入: S = "ADOBECODEBANC", T = "ABC"
输出: "BANC"

说明:

  • 如果 S 中不存这样的子串,则返回空字符串 ““。
  • 如果 S 中存在这样的子串,我们保证它是唯一的答案。

解:首先start,end两个左右指针,先右指针一直移动直到包含子串t;此时尝试移动左指针缩小范围,两种情况可以移动左指针,1这个字母不存在t中,2这个字母的已经存在的个数大于t中这个字母的个数;这两种情况分别处理,直到左指针不能再移动,计算一下长度,保存两个指针,继续移动右指针。

代码语言:javascript
复制
class Solution {
   public String minWindow(String s, String t) {
        if (s.isEmpty() || t.isEmpty()) {
            return "";
        }
        Map<Character, Integer> itemsFound = new HashMap<>();
        Map<Character, Integer> toBeFound = new HashMap<>();
        int count = 0;
        int minLen = Integer.MAX_VALUE;
        int l = -1;
        int r = -1;
        //记录字符串t中各字母出现次数
        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);
            if (toBeFound.containsKey(c)) {
                toBeFound.put(c, toBeFound.get(c) + 1);
            } else {
                toBeFound.put(c, 1);
            }
        }
        for (int start = 0, end = 0; end < s.length(); end++) {
            char e = s.charAt(end);
            //非目标串中字母,右指针继续右移
            if (!toBeFound.containsKey(e)) {
                continue;
            }
            if (itemsFound.containsKey(e)) {
                itemsFound.put(e, itemsFound.get(e) + 1);
            } else {
                itemsFound.put(e, 1);
            }
            if (itemsFound.get(e) <= toBeFound.get(e)) {
                count++;
            }
            if (count == t.length()) {//相等说明已经包含了t
                char ch = s.charAt(start);
                //两种情况可以移动start指针
                while (!toBeFound.containsKey(ch) || itemsFound.get(ch) > toBeFound.get(ch)) {
                    //第一种情况
                    if (!toBeFound.containsKey(ch)) {
                        start++;
                        ch = s.charAt(start);
                        continue;
                    }
                    //第二种情况
                    if (itemsFound.get(ch) > toBeFound.get(ch)) {
                        itemsFound.put(ch, itemsFound.get(ch) - 1);
                        start++;
                        ch = s.charAt(start);
                        continue;
                    }
                }
                if (end - start < minLen) {
                    minLen = end - start;
                    l = start;
                    r = end;
                }
            }
        }
        return l == -1 ? "" : s.substring(l, r + 1);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档