[LeetCode]76.Minimum Window Substring

本文介绍了一种在字符串S中寻找包含字符串T所有字符的最短子串算法,并提供了详细的实现思路与C++代码示例。该算法的时间复杂度为O(n),适用于面试和技术交流。

题目

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,
S = “ADOBECODEBANC”
T = “ABC”
Minimum window is “BANC”.

Note:
If there is no such window in S that covers all characters in T, return the emtpy string “”.

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

分析

详细些参考:[算法系列之二十二]包含T全部元素的最小子窗口

代码

/*--------------------------------------------
*   日期:2015-02-24
*   作者:SJF0115
*   题目: 76.Minimum Window Substring
*   网址:https://oj.leetcode.com/problems/minimum-window-substring/
*   结果:AC
*   来源:LeetCode
*   总结:
------------------------------------------------*/
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;


class Solution {
public:
    string minWindow(string S, string T) {
        int slen = S.size();
        int tlen = T.size();
        if(slen <= 0 || tlen <= 0){
            return "";
        }//if
        int minWinStart = 0,minWinEnd = 0;
        int minWinLen = INT_MAX;
        // 存储到目前为止遇到过的T中字符总数
        int count = 0;
        // 存储T中不同字符的总数
        int needFind[256] = {0};
        for(int i = 0;i < tlen;++i){
            ++needFind[T[i]];
        }//for
        // 存储到目前为止遇到过的不同字符的总数
        int hasFound[256] = {0};
        int val;
        for(int start = 0,end = 0;end < slen;++end){
            val = S[end];
            // 跳过不在T中的字符
            if(needFind[val] == 0){
                continue;
            }//if
            ++hasFound[val];
            if(hasFound[val] <= needFind[val]){
                ++count;
            }//if
            // 找到一个有效窗口
            if(count == tlen){
                int startVal = S[start];
                while(needFind[startVal] == 0 ||
                      hasFound[startVal] > needFind[startVal]){
                    if(hasFound[startVal] > needFind[startVal]){
                        --hasFound[startVal];
                    }//if
                    ++start;
                    startVal = S[start];
                }//while
                // 更新最小窗口
                int curWinLen = end - start + 1;
                if(curWinLen < minWinLen){
                    minWinLen = curWinLen;
                    minWinStart = start;
                    minWinEnd = end;
                }//if
            }//if
        }//for
        if(count != tlen){
            return "";
        }//if
        return S.substr(minWinStart,minWinEnd - minWinStart + 1);
    }
};

int main() {
    Solution solution;
    string S("acbbaca");
    string T("aba");
    cout<<solution.minWindow(S,T)<<endl;
}

运行时间

这里写图片描述

相似题目:

[经典面试题][搜狗]在一个字符串中寻找包含全部出现字符的最小字串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值