【LeetCode】76. Minimum Window Substring
Minimum Window Substring
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.
由于大小写字母的ASCII码不大于128,因此开辟两个数组存储信息。
needFind数组存储T字符串每个字符出现次数。例如:needFind['A']=5意为T中A出现5次。
Found数组存储S字符串在[begin,end]窗口内每个字符出现次数。
算法核心思想如下:
在保证[begin,end]窗口包含T中所有字符的条件下,延伸end,收缩begin。
进行一次扫描后,记录符合条件的最小窗口(end-begin+1)表示的字符串。
有个问题:怎样才知道[begin,end]窗口包含了T中所有字符?
我使用count记录剩余“有效”字符数,当count达到0时,即可说明[begin,end]包含了T。
注意:“有效”的意思是指,当前延伸得到的S[end]字符,使得[begin,end]更进一步包含T,而不是重复劳动。
比如说,T="a", [begin,end]已经包含"a",再延伸得到"aa",只是无效操作,并没有使得[begin,end]更接近T,有效字符数仍为1.
class Solution {
public:
    string minWindow(string S, string T) {
        int begin = ;
        int end = ;
        int minbegin = ;
        int minend = ;
        int minSize = INT_MAX;
        vector<int> needFind(, );
        vector<int> Found(, );
        for(int i = ; i < T.size(); i ++)
            needFind[T[i]] ++;
        Found[S[]] ++;
        int count = T.size();
        if(needFind[S[]] >= Found[S[]])
            count --;
        while(true)
        {
            if(count == )
            {//shrink begin
                while(Found[S[begin]] > needFind[S[begin]])
                {
                    Found[S[begin]] --;
                    begin ++;
                }
                int size = end-begin+;
                if(size < minSize)
                {
                    minbegin = begin;
                    minend = end;
                    minSize = size;
                }
            }
            if(end < S.size())
            {
                end ++;
                Found[S[end]] ++;
                if(needFind[S[end]] >= Found[S[end]])
                    count --;
            }
            else
                break;
        }
        if(minSize != INT_MAX)
            return S.substr(minbegin, minSize);
        else
            return "";
    }
};

【LeetCode】76. Minimum Window Substring的更多相关文章
- 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...
 - 【一天一道LeetCode】#76. Minimum Window Substring
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
 - 【leetcode】963. Minimum Area Rectangle II
		
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
 - 刷题76. Minimum Window Substring
		
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
 - 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
		
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
 - 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 ...
 - 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
		
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
 - [LeetCode] 76. Minimum Window Substring 最小窗口子串
		
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
 - [LeetCode] 76. Minimum Window Substring 解题思路
		
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
 
随机推荐
- JVM:如何分析线程堆栈
			
英文原文:JVM: How to analyze Thread Dump 在这篇文章里我将教会你如何分析JVM的线程堆栈以及如何从堆栈信息中找出问题的根因.在我看来线程堆栈分析技术是Java EE产品 ...
 - Ext Connection
			
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
 - N个富文本编辑器/基于Web的HTML编辑器
			
转自:http://www.cnblogs.com/lingyuan/archive/2010/11/15/1877447.html 基于WEB的HTML 编辑器,WYSIWYG所见即所得的编辑器,或 ...
 - thymleaf th:if标签
			
1.概念 <table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOC ...
 - ContentProvider的那些小事(纯结论)
			
一.ContentProvider背景 Android系统是基于Linux系统内核来进行开发的,在Linux中,文件具有一系列的属性,其中最重要的莫过于文件权限了.关于文件权限,其实就是文件的读写,执 ...
 - 在Ubuntu 13.04下的安装eclipse
			
来源:http://www.cnblogs.com/lanxuezaipiao/p/3325628.html 一.eclipse安装过程 首先确保在安装eclipse之前已经安装好Java虚拟机 1. ...
 - Scrum 思考
			
下个月就要离职,所以这个月特别清闲,上班时间都在上网看书,偶然在Startup News的一篇文章(http://blog.devtang.com/blog/2013/06/17/startup-an ...
 - SVM训练结果参数说明 训练参数说明 归一化加快速度和提升准确率 归一化还原
			
原文:http://blog.sina.com.cn/s/blog_57a1cae80101bit5.html 举例说明 svmtrain -s 0 -?c 1000 -t 1 -g 1 -r 1 - ...
 - (剑指Offer)面试题5:从尾到头打印链表
			
题目: 输入一个链表的头结点,从尾到头反过来打印每个结点的值. 链表结点定义: struct ListNode{ int value; ListNode* pNext; }; 思路: 1.改变链表结构 ...
 - C-IDE使用指南
			
HI 您好: 亲爱的学员,本文章是基于C-IDE的入口使用指南,您能够查看demo项目来了解C-IDE详细操作哦~ 如有疑问您可提交反馈来咨询,或扫描下方二维码增加官方微信群.我们会认真对待且具体回 ...