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 最小字符窗口的更多相关文章

  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]76. 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

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

  4. [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 ...

  5. [LeetCode] Minimum Window Subsequence 最小窗口序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...

  6. [leetcode]Minimum Window Substring @ Python

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

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

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

  8. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

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

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

随机推荐

  1. 快速写一个babel插件

    es6/7/8的出现,给我们带来了很多方便,但是浏览器并不怎么支持,目前chrome应该是支持率最高的,所以为了兼容我们只能把代码转成es5,这应该算是我们最初使用babel的一个缘由,随着业务的开发 ...

  2. 【SpringCloud】第五篇: 路由网关(zuul)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  3. Linux命令应用大词典-第14章 显示登录用户

    14.1 w:详细查询已登录当前计算机的用户 14.2 who:显示已登录当前计算机用户的简单信息 14.3 whoami:显示与当前的有效ID相关联的用户名 14.4 logname:显示当前用户的 ...

  4. 【转】cocos2dx3.2学习笔记之Director(导演类)

    转载:https://blog.csdn.net/u013435551/article/details/38579747 在Cocos2d-x中,把统筹游戏大局的类抽象为导演类(Director),D ...

  5. BOM / URL编码解码 / 浏览器存储

    BOM 浏览器对象模型 BOM(Browser Object Model) 是指浏览器对象模型,是用于描述这种对象与对象之间层次关系的模型,浏览器对象模型提供了独立于内容的.可以与浏览器窗口进行互动的 ...

  6. 【MySQL解惑笔记】Navicat 无法远程连接MySQL数据库

    安装好Navicat之后远程连接MySQL数据库出现以下报错截图: 出现以上截图怀疑是mysql用户权限不够: GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.1 ...

  7. Machine Learning笔记整理 ------ (五)决策树、随机森林

    1. 决策树 一般的,一棵决策树包含一个根结点.若干内部结点和若干叶子结点,叶子节点对应决策结果,其他每个结点对应一个属性测试,每个结点包含的样本集合根据属性测试结果被划分到子结点中,而根结点包含样本 ...

  8. opencv-学习笔记(2)

    opencv-学习笔记(2) 这章记录了 获取像素点,改变像素点 获取图像的属性(行,列,通道数,数据类型) roi感应区 拆分以及合并图像通道 边缘扩充 opencv获取像素点,改变像素点 ---- ...

  9. leetcode个人题解——#19 Remove Nth Node From End of List

    思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...

  10. 四、oracle 用户管理二

    一.使用profile管理用户口令概述:profile是口令限制,资源限制的命令集合,当建立数据库时,oracle会自动建立名称为default的profile.当建立用户没有指定profile选项时 ...