[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 ...
随机推荐
- 如何用istio实现应用的灰度发布
Istio为用户提供基于微服务的流量治理能力.Istio允许用户按照标准制定一套流量分发规则,并且无侵入的下发到实例中,平滑稳定的实现灰度发布功能. 基于华为云的Istio服务网格技术,使得灰度发布全 ...
- 51单片机实现定时器00H-FFH、定时器000-255
#include< reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 = 0x94; sfr ...
- AVL树和伸展树 -数据结构(C语言实现)
读数据结构与算法分析 AVL树 带有平衡条件的二叉树,通常要求每颗树的左右子树深度差<=1 可以将破坏平衡的插入操作分为四种,最后通过旋转恢复平衡 破坏平衡的插入方式 描述 恢复平衡旋转方式 L ...
- 爬虫1.6-selenium+HeadlessChrome
目录 爬虫-selenium+HeadlessChrome 1. 浏览器处理步骤 2. headless-chrome初体验 3. 实战爬取淘宝镇.街道信息 爬虫-selenium+HeadlessC ...
- (转)GEM -次表面散射的实时近似
次表面散射(Subsurface Scattering),简称SSS,或3S,是光射入非金属材质后在内部发生散射, 最后射出物体并进入视野中产生的现象, 即光从表面进入物体经过内部散射,然后又通过物体 ...
- Tensorflow学习第1课——从本地加载MNIST以及FashionMNIST数据
很多Tensorflow第一课的教程都是使用MNIST或者FashionMNIST数据集作为示例数据集,但是其给的例程基本都是从网络上用load_data函数直接加载,该函数封装程度比较高,如果网络出 ...
- CryptoZombies学习笔记——Lesson4
第四课主要介绍payable函数相关. chapter1: payable修饰函数 以太坊允许同时调用函数和eth转账.msg.value显示发送到合约的以太币数,ether是内置整型数.如果函数没有 ...
- 软工2017第六周团队协作——个人PSP
10.20 --10.26本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间 结束时间 中断时间 实际用 ...
- DAY7敏捷冲刺
站立式会议 工作安排 (1)服务器配置 服务器端项目结构调整 (2)数据库配置 单词学习记录+用户信息 (3)客户端 客户端项目结构调整,代码功能分离 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面, ...
- java — JVM调优
数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...