[Leetcode] 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.
题意:在S中寻找最小包含T的子串,不管字母顺序。
思路:采用桶排序。先将T中所有的元素放入hash表中,然后遍历字符串S。如果当前字符在hash表中没有,说明这个不在T中,则直接跳过。若在,则将hash表中对应的字符的个数减1,说明T中的字符已经找到一个了;此时,若对应字符的个数不小于0,说明这个字符是有效的。什么意思了?如:字符串S=“ABAC”找T=“AC”,第一次,找到“A”时,hash表中A对应的个数变为0,即为有效的A,计数器count记下了A的个数;再次遇到A时,说明T只有一个A,且已经被记下了,所以不需要再次计数。即,忽略重复的,找到T中全部的就行。那明明是第二个A更符合题意,为什么要记下第一个A,这个会在后面慢慢的说明。
这样直到T的中的每个字符都被记下了,且只记下一次。这时,我们更新最小的合乎题意的子字符串的长度。这时,我们遇到一个问题,就是,如果开始时,有很多字符不是T中的,但是我们记下的最小字符串包括这些,怎么办?我们只需不断的将左指针向右移动,直到遇到第一个在T中的字符,这个过程中,我们不断的更新minlen长度,这样我们就得到了这次的复合题意的最下子字符串的长度。
那么我们如何得到下一个符合题意的长度?此时,我们只需将最左端字符在hash表中的个数加1,然后count减1,就实现了,重新寻找下一个子字符串的循环。因为,会遍历完S,所以之前说的遇到第二个A时,会重新更新minlen,所以之前只用记住一个A的。参考了Grandyang的博客。代码如下:
class Solution {
public:
string minWindow(string S, string T)
{
if(T.size()>S.size()) return "";
string res="";
int left=,count=,minLen=S.size()+;
unordered_map<char,int> m;
for(int i=;i<T.size();++i)
{
if(m.find(T[i]) !=m.end())
++m[T[i]];
else
m[T[i]]=;
}
for(int right=;right<S.size();++right)
{
if(m.find(S[right]) !=m.end())
{
--m[S[right]];
if(m[S[right]]>=)
++count;
while(count==T.size())
{
if(right-left+<minLen)
{
minLen=right-left+;
res=S.substr(left,minLen);
}
if(m.find(S[left]) !=m.end())
{
++m[S[left]];
if(m[S[left]]>)
--count;
}
left++;
}
}
}
return res;
}
};
解题思路:其实就是通过双指针维持一个Window,窗口右指针向右扩张用来找到包含子串为目的,窗口左指针向右收缩以使子串最小。典型的滑动窗口方法的实现。
也可以用数组来替代hash表,参考 曲高和寡_健的博客,代码如下:
class Solution {
public:
string minWindow(string S, string T)
{
int sLen=S.size(),tLen=T.size();
if(sLen==||tLen==)
return "";
vector<int> sHash(,),tHash(,);
for(int i=,i<tLen;++i)
{
tHash[T[i]]++;
}
int beg=-,end=sLen,count=,minLen=sLen;
for(int i=,start=i;i<sLen;++i)
{
++sHash[S[i]];
if(sHash[S[i]]<=tHash[S[i]])
++count;
if(count==tLen)
{
while(start<i&&sHash[S[start]]>tHash[S[start]])
{
--sHash[S[start]];
start++;
}
if(i-start<minLen)
{
minLen=i-start;
beg=start;
end=i;
}
--sHash[S[start++]]; //寻找下一个符合要求的子字符串
--count;
}
}
if(beg==-)
return "";
return S.substr(beg,end-beg+);
}
};
这种方法看的更为清晰些。
[Leetcode] minimum window substring 最小字符窗口的更多相关文章
- [LeetCode] 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 ...
- Leetcode 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 ...
- [LeetCode] Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [leetcode]Minimum Window Substring @ Python
原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...
- [LeetCode] Minimum Window Substring 散列映射问题
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
- LeetCode()Minimum Window Substring 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...
随机推荐
- Machine Learning Basic Knowledge
常用的数据挖掘&机器学习知识(点) Basis(基础): MSE(MeanSquare Error 均方误差),LMS(Least MeanSquare 最小均方),LSM(Least Squ ...
- 成员变量:对象vs指针
一旦类初始化,那么对象必然会被创建,指针则可以在需要时候再去初始化所指向.
- VS Help Viewer 显示内容为HTML源码的问题
万恶的IE10 为了学习,安装了一套Windows Server 2012+SQL 2012+VS 2012的环境,整体感觉还不错,只是在使用Help Viewer查看帮助的时候,发现显示内容居然为H ...
- 「专题训练」Air Raid(HDU-1151)
题目 在一个城市里有\(n\)个地点和\(k\)条道路,道路是无环的(也就是说一定可以二分染色--回路长度为偶数0),现在伞兵需要去n个地点视察,只能沿着路的方向走,问最少需要多少伞兵. 分析 这是什 ...
- apache+php+mysql开发环境搭建
一.Apache 因为Apache官网只提供源代码,如果要使用必须得自己编译,这里我选择第三方安装包Apache Lounge. 进入Apachelounge官方下载地址:http://w ...
- jieba结巴分词
pip install jieba安装jieba模块 如果网速比较慢,可以使用豆瓣的Python源:pip install -i https://pypi.douban.com/simple/ jie ...
- Linux命令应用大词典-第8章 日期和时间
8.1 cal:显示日历信息 8.2 date:显示和设置系统日期和时间 8.3 hwclock:查看和设置硬件时钟 8.4 clock:查看和设置硬件时钟 8.5 clockdiff:主机之间测量时 ...
- Shader Forge学习
最近学习了一下shader forge,一个屌屌哒插件用来生成shader.尽管其降低了制作shader的难度,但是真的想做出满意的shader的话还是得有一定的shader基础.但是仅仅是做出一些简 ...
- Asp.net之数组应用
string[] abc=new string[8]{"1","2","3","4","1",&qu ...
- Bus of Characters(栈和队列)
In the Bus of Characters there are nn rows of seat, each having 22 seats. The width of both seats in ...