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. BZOJ1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

    1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 509  Sol ...

  2. 五大P2P平台费用一览

    我看版上谈P2P的挺多的,正好我也投了点P2P, 今天看到一个不错的图 欢迎版上朋友讨论       -- 不忧不惧

  3. 2014-08-13 SQL语句之Left Join

    今天是在吾索实习的第26天.这天在处理数据库数据的时候发现了一个不错的语句就是Left Join,即左连接.  其功能是:即使右表中没有匹配,也从左表返回所有的行.也就是说,显示的行数与左表一致,且当 ...

  4. AES的S-BOX构造优化

    之前写过SBOX的构造,后来看到别人的优秀思路,借鉴过来重新改了一点. 原文地址:http://www.cnblogs.com/7hat/p/3383546.html 主要是将矩阵运算改为列运算之和, ...

  5. URAL 1244

    题目大意:给出一个正整数M,给出N个正整数ai,让你在这些数中挑出一些数组成M的一个划分,如果符合条件的划分数超过两个,输出:-1,如果没有输出:0,如果有且仅有一个:则按顺序输出剩下的数的序号. 例 ...

  6. spring + shiro + cas 实现sso单点登录

    sso-shiro-cas spring下使用shiro+cas配置单点登录,多个系统之间的访问,每次只需要登录一次,项目源码 系统模块说明 cas: 单点登录模块,这里直接拿的是cas的项目改了点样 ...

  7. JAVA的RSA加密算法工具类

    须要用到一个jar http://www.bouncycastle.org/latest_releases.html 须要注意的问题 JS用同一秘钥生成的密文用java解密出来是逆序的,即js加密12 ...

  8. android中定位光标位置

    edittext.setSelection(int); edittext.setText(123);//设置edittext中的内容 edittext.setSelection(123.length( ...

  9. C++经典题目:有n个人围成一圈,顺序排号,然后数数进行淘汰的解法和一些思考

    问题描述: 有n个人围成一圈,顺序排号.从第一个人开始报数(1~3报数),凡报到3的人退出圈子,问最后留下的人原来排在第几号. 分析: 首先由用户输入人数n,然后对这n个人进行编号[因为如果不编号的话 ...

  10. html中编写js的方式

    第一种:引用外部的js文件 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...