LeetCode 076.Minimum Window Substring 最小覆盖子串

题目描述

给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串。

示例:

输入: S = “ADOBECODEBANC”, T = “ABC”
输出: “BANC”
说明:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-window-substring

思路

双指针,动态地维护两个指针。尾指针不断往后面扫,直到包含所有的子串,然后开始收缩头指针,直到不能收缩为止。
在这里插入图片描述

c++

class Solution {
public:
    string minWindow(string s, string t) {
        if(s.empty() || s.size() < t.size())   return "";
        const int ASCII_MAX = 256;
        int expectCount[ASCII_MAX];
        int appearedCount[ASCII_MAX];
        fill(expectCount, expectCount + ASCII_MAX, 0);
        fill(appearedCount, appearedCount + ASCII_MAX, 0);
        for(int i = 0; i < t.size(); i++){
            expectCount[t[i]] += 1;
        }
        int wntStart = 0, wntEnd = 0;
        int minLength = INT_MAX, minStart = 0;
        int appeared = 0;
        for(; wntEnd < s.size(); wntEnd++){
            if(expectCount[s[wntEnd]] > 0){
                appearedCount[s[wntEnd]] ++;
                if(appearedCount[s[wntEnd]] <= expectCount[s[wntEnd]])
                    appeared ++;
            }
            if(t.size() == appeared){
                while(appearedCount[s[wntStart]] > expectCount[s[wntStart]] || expectCount[s[wntStart]] == 0){
                    appearedCount[s[wntStart]] --;
                    wntStart ++;
                }
                if(minLength > (wntEnd - wntStart + 1)){
                    minLength = wntEnd - wntStart + 1;
                    minStart = wntStart;
                }
            }
        }
        if(minLength == INT_MAX)   return "";
            else
                return s.substr(minStart, minLength);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值