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的字符后,然后再收缩头指针,直到不能再收缩为止。最后记录所有可能的情况中窗口最小的

class Solution {
public:
string minWindow(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> expect(, );
vector<int> appear(, );
int count = ;
int start = ;
int minstart= ;
int minLen = INT_MAX; for(int i = ; i < T.size(); i++)expect[T[i]]++; for(int i = ; i < S.size() ; i++)
{
if(expect[S[i]] > ){
appear[S[i]]++;
if(appear[S[i]] <= expect[S[i]])
count++;
}
if(count == T.size()){
while(expect[S[start]] == || appear[S[start]] > expect[S[start]]){
appear[S[start]]--;
start++;
}
if(minLen > (i - start +) ){
minLen = i - start + ;
minstart = start;
}
}
} if(minLen == INT_MAX) return string(""); return S.substr(minstart, minLen);
}
};

LeetCode_Minimum 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 ...

随机推荐

  1. 动态SQL使用绑定变量

    SQL> begin   for i in 1..1000000    loop     execute immediate 'insert into p1 values(i)' ;     c ...

  2. 自动删除n天前日志

    自动删除n天前日志 linux 是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种 爽快的事情.不用你去每 ...

  3. 设计模式(七):Adapter 适配器模式 -- 结构型模式

    1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相关库的发展和进化.  例子1:iphone4,你即可以 ...

  4. 黑马程序员_Java集合框架

    集合类 1,为什么出现集合类? 面向对象语言对食物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式. 2,数组和集合类同是容器,有何不同? 数组 ...

  5. android调用系统自带的的浏览器搜索关键字

    //调用系统的浏览器搜索详情 public void jumpBrowser(String value) { /* 取得网页搜寻的intent */ Intent search = new Inten ...

  6. [HNOI 2013] 旅行 (数学)

    感觉此题难啊,数学还是太渣了,看了半天的题解才算明白了点儿. 题目大意 给一个长度为n且仅由1和-1组成的序列ai, i = 1, 2, ..., n,每个位置都有另一个值vi,要求用某种方案将序列划 ...

  7. libvirtsAPI

    mongodb远程服务器连接 mongo -uroot -p321 master.puppet.org:27017/admin

  8. Findbugs初探-使用idea获取findbugs插件

    /* * 整天忙于无限重复的黑盒测试,不论是写脚本,还是手工测,都难以脱离黑&&灰盒测试,所以忽然间想,大学时期学过的所有课程就这样扔掉吗?不好!既然选择了做测试,那么就要一直坚持下去 ...

  9. springBoot学习

    http://blog.csdn.net/xiaoyu411502/article/details/47864969 博客: http://blog.csdn.net/xiaoyu411502/art ...

  10. nginx 配置访问正则匹配

    server{ listen 80; server_name api.zyy.com; root /var/www/api_zyy; index index.php; location ~ /asse ...