题目描述

给出两个字符串S和T,要求在O(n)的时间复杂度内在S中找出最短的包含T中所有字符的子串。
例如:
S ="ADOBECODEBANC"
T ="ABC"
找出的最短子串为"BANC".
注意:
如果S中没有包含T中所有字符的子串,返回空字符串 “”;
满足条件的子串可能有很多,但是题目保证满足条件的最短的子串唯一。

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.

示例1

输入

复制

"ADOBECODEBANC","ABC"

输出

复制

"BANC"

链接:https://www.nowcoder.com/questionTerminal/c466d480d20c4c7c9d322d12ca7955ac?f=discussion
来源:牛客网

class Solution {
public:
    //维持一个窗口滑动,左边是left,右边是right,然后判断是否包含T
    string minWindow(string S, string T)
    {
        string result;
        if(!S.size() || !T.size())
        {
            return result;
        }
        map<char, int>Tmap;   //存储T字符串里字符,便于与S匹配
        int left = 0;       //左边窗口,起始为0
        int count = 0;      //计数器,对窗口内T串字符数量进行计数,起始为0
        //装载T串
        int minlen = S.size() + 1;      //最小窗口,便于比较最后取最小的,初始化取最大
        for(int i = 0; i < T.size(); ++i)
        {
            Tmap[T[i]]++;
        }
        //移动右边窗口
        for(int right = 0; right < S.size(); ++right)
        {
            if(Tmap.find(S[right]) != Tmap.end())   //当窗口内部有T中的字符
            {
                if(Tmap[S[right]] > 0)
                {
                    count++;            //计数器+1
                }
                Tmap[S[right]]--;   //去掉包含的,剩下都是没包含的
                while(count == T.size())//当窗口内有T的所有字符,就要开始移动左窗口啦
                {
                    if(Tmap.find(S[left]) != Tmap.end())
                    {
                        //好了,这就是一个包含字符串的窗口范围:left ~ right,
                        //判断是否比当前窗口还小,是就取串
                        if(minlen > right - left + 1)
                        {
                            //更新窗口大小
                            minlen = right -left + 1;
                            result = S.substr(left, right - left + 1);
                        }
                        //舍弃窗口左边字符串,继续移动窗口
                        Tmap[S[left]]++;
                        if(Tmap[S[left]] > 0)            //如果左边连续相同,则count不递减,窗口需要继续向右移动
                        {
                            count--;
                        }
                    }
                    left++;             //移动左窗口
                }
            }
        }
        return result;
    }
};


leetcode73:minmum-window-substring的更多相关文章

  1. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  2. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  3. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

  4. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  5. 【LeetCode】76. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  6. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  7. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [Leetcode][JAVA] Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. Java for LeetCode 076 Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  10. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

随机推荐

  1. CentOS7 执行 service iptables save 报错 The service command supports only basic LSB actions xxxxxx

    现象描述 在 CentOS 7.6.1810 下执行 service iptables save 命令,出现如下错误: [root@test ~]# service iptables save The ...

  2. junit调试(No tests found matching )

    使用junit调试程序时报错:initializationError(org.junit.runner.manipulation.Filter)java.lang.Exception: No test ...

  3. 写了多年代码,你会 StackOverflow 吗

    写了多年代码,你会 StackOverflow 吗 Intro 准备写一个傻逼代码的系列文章,怎么写 StackOverflow 的代码,怎么写死锁代码,怎么写一个把 CPU 跑满,怎么写一个 Out ...

  4. MeteoInfoLab脚本示例:地图投影

    在用axesm函数创建地图坐标系的时候可以指定地图投影(设置projinfo参数),地图投影可以通过projinfo函数来创建,里面的参数依据proj4投影字符串,可以参考此网页:http://rem ...

  5. MeteoInfoLab脚本示例:LaTeX写数学公式

    LaTeX是排版常用的语法,科学计算软件中也常用它来写数学公式(比如MatLab, Matplotlib等),MeteoInfo通过调用JMathLaTeX库也可以实现这样的功能.LaTeX的语法介绍 ...

  6. day24 Pyhton学习 反射

    一.isinstance,type,issubclass issubclass() 这个内置函数可以帮我们判断x类是否是y类的子类 issubclass(x,y) class Base: pass c ...

  7. pytest文档44-allure.dynamic动态生成用例标题

    前言 pytest 结合 allure 描述用例的时候我们一般使用 @allure.title 和 @allure.description 描述测试用例的标题和详情. 在用例里面也可以动态更新标题和详 ...

  8. ngx_align 值对齐宏

    ngx_align 值对齐宏 ngx_align 为nginx中的一个值对齐宏.主要在需要内存申请的地方使用,为了减少在不同的 cache line 中内存而生. // d 为需要对齐的 // a 为 ...

  9. Java9系列第7篇:Java.util.Optional优化与增强

    我计划在后续的一段时间内,写一系列关于java 9的文章,虽然java 9 不像Java 8或者Java 11那样的核心java版本,但是还是有很多的特性值得关注.期待您能关注我,我将把java 9 ...

  10. JavaSE学习笔记02运算符、帮助文档生成与Scanner输入

    1. 运算符 1. 算术运算符:+,-,*,/,%,++,-- //二元运算符 int a = 10; int b = 20; int c = 25; int d = 25; System.out.p ...