[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 ...
随机推荐
- Java开发工程师(Web方向) - 04.Spring框架 - 第3章.AOP技术
第3章--AOP技术 Spring框架 - AOP概述 笔记https://my.oschina.net/hava/blog/758873Spring框架 - AOP使用 笔记https://my.o ...
- Git命令使用大全
一前言 最近公司在使用vue和WebAPI前后端分离的项目开发,使用的代码管理工具是git,刚开始使用的时候前端的vue文件还比较好处理,但是后端的C#文件在每一次自己编译之后上传都会和其他小伙伴的代 ...
- 小程序页面的四种文件(JSON、WXML、WXSS、JS)加载顺序
一个小程序页面由四种文件组成: 1)json 页面配置文件 2)js 页面逻辑文件(必需) 3)wxml 页面结构文件(必需) 4)wxss 页面样式文件 这四个文件的加载顺序: 第一步: 加载页面j ...
- ntp服务:实现时间同步
一. 引言 目前的项目为分布式系统,采用dubbo+zookeepe,排查BUG,发现各个服务器的时间不一致,遂网上查找资源,使得时间保持一致. 二. 步骤 1)以第一台服务器为“服务端”,其他台服务 ...
- 关于C#中如何使用wmi获得操作系统信息?
最近项目中用到了windows server 2012操作系统中的存储池和ISCSI Disk的技术.前期,我们整个操作都是用power shell脚本去实现了.带来了不方便,后期要使用wmi API ...
- vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT-x,但 ...
- 最小生成树(Kruskal和Prim算法)
关于图的几个概念定义: 关于图的几个概念定义: 连通图:在无向图中,若任意两个顶点vi与vj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vi与vj都有路 ...
- scanf格式控制符的完整格式
scanf格式控制的完整格式: % * m l或h 格式字符 ①格式字符与printf函数中的使用方式相同,以%d.%o.%x.%c.%s.%f.%e,无%u格式.%g ...
- com技术学习
百度百科概念 COM是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术.在COM构架下,人们可以开发出各种各样的功能专一的组件,然后将它们按照需要组合起来,构成复杂的应 ...
- spring框架(1)— 依赖注入
依赖注入 spring核心容器就是一个超级大工厂,所以的对象(数据源.hibernate SessionFactory等基础性资源)都会被当做spring核心容器的管理对象——spring把容器中的一 ...