题目描述

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所有字母的最小窗口。

注意:S=bba t=ba。此时right=2时,left=0,移动left时会++m[b],但是此时窗口内仍旧有一个b。所以在right移动时,m[b]=-1,即只要包含即--,而只有m[b]>=0时,才是有效的,才会++count。同样,在移动left时也是,只有m[b]>0时,才会count--。

 
这道题是字符串处理的题目,和Substring with Concatenation of All Words思路非常类似,同样是建立一个字典,然后维护一个窗口。区别是在这道题目中,因为可以跳过没在字典里面的字符(也就是这个串不需要包含且仅仅包含字典里面的字符,有一些不在字典的仍然可以满足要求),所以遇到没在字典里面的字符可以继续移动窗口右端,而移动窗口左端的条件是当找到满足条件的串之后,一直移动窗口左端直到有字典里的字符不再在窗口里。在实现中就是维护一个HashMap,一开始key包含字典中所有字符,value就是该字符的数量,然后遇到字典中字符时就将对应字符的数量减一。算法的时间复杂度是O(n),其中n是字符串的长度,因为每个字符再维护窗口的过程中不会被访问多于两次。空间复杂度则是O(字典的大小),也就是代码中T的长度。代码如下:

 class Solution {
public:
string minWindow(string S, string T) {
if(S.length()==)
return "";
map<char, int> m;
for(int i=;i<T.length();i++){
if(m.find(T[i])!=m.end())
m[T[i]]++;
else
m[T[i]]=;
}
int left=,right=,min=S.length()+,minStart=,count=;
for(right=;right<S.length();right++){
if(m.find(S[right])==m.end())
continue;
m[S[right]]--;
if(m[S[right]]>=){
count++;
}
while(count==T.length()){
if((right-left+)<min){
min=right-left+;
minStart=left;
}
if(m.find(S[left])!=m.end()){
m[S[left]]++;
if(m[S[left]]>)
count--;
}
left++;
}
}
string res="";
if(min!=S.length()+){
res=S.substr(minStart,min);
}
return res;
}
};

LeetCode-Minimum Window Substring -- 窗口问题的更多相关文章

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

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

  2. [Leetcode] minimum window substring 最小字符窗口

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

  3. [leetcode]Minimum Window Substring @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...

  4. [LeetCode] Minimum Window Substring 散列映射问题

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  5. Leetcode Minimum Window Substring

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

  6. LeetCode()Minimum Window Substring 超时,但觉得很清晰。

    我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...

  7. Minimum Window Substring @LeetCode

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

  8. 【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 ...

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

  10. leetcode76. Minimum Window Substring

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

随机推荐

  1. 软中断网卡处理&Linux高性能外部设备处理机制&SMP

    转载:http://blog.csdn.net/freas_1990/article/details/9238183 看了一些linux网卡驱动的处理技术,对有些概念还是无法理解,突然搜到这篇文章,挺 ...

  2. 我使用的Sublime插件及配置

    我使用的Sublime插件及配置 增强型插件 Package Control 快捷键ctrl+~,调出命令行,运行: import urllib.request,os,hashlib; h = '29 ...

  3. Delphi中获取文件大小

    大概有这些方法可以获得文件大小FileSizeByName(需要引用IdGlobal单元)GetFileSizeFileSize(不能获得正在使用的文件大小)FileSeekTFileStream.S ...

  4. [BZOJ1052][HAOI2007]覆盖问题 二分+贪心

    1052: [HAOI2007]覆盖问题 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2053  Solved: 959 [Submit][Sta ...

  5. AC日记——Little Elephant and Function codeforces 221a

    A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...

  6. Redis主从复制、哨兵模式

    1.部署主从 环境:主IP:10.0.0.15,端口6379;从IP:10.0.0.16,端口6379. 原理:基于RDB持久化的功能来实现主从复制的功能. a.linux-redis1(10.0.0 ...

  7. Linux是32位还是64位

    命令行输入 file /bin/ls 显示 /sbin/init: ELF 64-bit LSB executable, x86-64 ...   则为64位 file /sbin/init /sbi ...

  8. 队列 LinkedBlockingQueue

    1 api     java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,是一个阻塞的线程安全的队列,底层采用链表实现.             Li ...

  9. 自己写Tiny6410的Bootloader总结!

    1.由于Tiny6410 2G版的Nand flash(K9GAG08U0E)的页大小是8K的,但是s3c6410芯片设置为nand flash启动时先从nand flash复制8K代码到片内内存中去 ...

  10. 删除VisualStudio 2013中的 "send Feedback" 按钮

    在VisualStudio 2013中,在标题栏中增加了一个 "send Feedback" 按钮,用于给微软发送Bug和回馈(或者一个哭脸和笑脸).这个按钮对于开发来说基本上没用 ...