题目:


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.

Hide Tags

Hash Table Two Pointers String


     在字符串 S 中查找最小的窗口,使窗口中全部包含字符串T 中的字符,(不按顺序),需要注意的地方:
  • T 中的字符可以重复,窗口需要重复包含。
  • 测试 实验中的是SCII字符 ,所以可以用128位数组代替 hash table。
  • leetcode  c++ 中不能用 hash_map。

思路:

  1. 使用一个映射int need(hash table 或者 128位数组) 存储T中个字符需要的个数,因为需求可能为负,所以用int。
  2. 使用一个映射bool exitst(hash table 或者 128位数组) 存储T中个字符是否存在,使用hashtable 也构造这个,因为find 是遍历查找的。
  3. 用一个var 记录窗口中符合T 中char 的个数。
  4. 用下表start、last 标记窗口位置,start 指向窗口内第一个char,last 指向窗口外右边第一个char(包括结尾)。
  5. 每次循环加入或删除(记录一个flag)一个字符,如果不存在便继续循环。
  6. 通过判断加入删除flag进行操作,更新need 表,更新var, 如果等于T 的长度,更新返回记录。
  7. 循环结束判断能否查找。
 下面是我写的,需要的时间复杂度为O(length(S)),即O(n),空间复杂度为O(length(T)):
 #include <string>
#include <hash_map>
#include <iostream>
#include <map>
using namespace std;
using namespace __gnu_cxx; class Solution {
public:
string minWindow(string S, string T) {
int numS = S.length(),numT = T.length();
if(numS<||numT<) return "";
int WinStart=,WinLast=,WinCount =,retStart,leng=INT_MAX;
hash_map<char, int > need;
hash_map<char, bool > exist;
for(int i =;i<numT;i++){
need[T[i]]++;
exist[T[i]] = true;
} bool addorminus;
char curchar;
while(WinStart<=numS-numT){
if(WinCount!=numT&&WinLast<numS){
addorminus = true;
curchar = S[WinLast++];
}
else{
addorminus = false;
curchar = S[WinStart++];
}
if(!exist[curchar]) continue;
if(addorminus){
if(need[curchar]>) WinCount++;
need[curchar]--;
if(WinCount==numT&&leng>WinLast - WinStart){
retStart = WinStart;
leng = WinLast - WinStart;
}
}
else{
if(WinCount==numT&&leng>WinLast - WinStart+){
retStart = WinStart-;
leng = WinLast - WinStart+;
}
need[curchar] ++;
if(need[curchar]>) WinCount--;
}
}
if(leng==INT_MAX)
return "";
return S.substr(retStart,leng);
}
}; int main()
{
string s = "1A123BAC1";
string t = "AABC";
Solution sol; string ret = sol.minWindow(s,t);
cout<<ret<<endl;
return ;
}
 leetcode 讨论里面有另外一个实现,也是O(n),实现类似,不同的是循环内是通过判断窗口中已经有T中字符的个数来进行 add or delete,我写的是通过目标操作字符来进行,逻辑上没有ta写的方便。
 https://oj.leetcode.com/discuss/10337/accepted-o-n-solution
 

 class Solution {
public:
string minWindow(string S, string T) {
if (S.empty() || T.empty())
{
return "";
}
int count = T.size();
int require[] = {};
bool chSet[] = {false};
for (int i = ; i < count; ++i)
{
require[T[i]]++;
chSet[T[i]] = true;
}
int i = -;
int j = ;
int minLen = INT_MAX;
int minIdx = ;
while (i < (int)S.size() && j < (int)S.size())
{
if (count)
{
i++;
require[S[i]]--;
if (chSet[S[i]] && require[S[i]] >= )
{
count--;
}
}
else
{
if (minLen > i - j + )
{
minLen = i - j + ;
minIdx = j;
}
require[S[j]]++;
if (chSet[S[j]] && require[S[j]] > )
{
count++;
}
j++;
}
}
if (minLen == INT_MAX)
{
return "";
}
return S.substr(minIdx, minLen);
}
};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

[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 @ Python

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

  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] 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 Substring 超时,但觉得很清晰。

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

  6. Minimum Window Substring @LeetCode

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

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

  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. 53. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

随机推荐

  1. skynet 学习笔记-sproto模块(2)

    云风在skynet中继承了sproto的传输协议,对比protobuf的好处是,能明文看到传输内容,而且skynet不需要protobuf这么功能,所以云风也建议在lua层使用sproto来作为sky ...

  2. 解决cocos游戏安卓release版本闪退问题

    在cocos中偶尔会遇到闪退的问题,特别是android和ios系统下的闪退就特别难处理了, 虽然说能使用xcode和eclipse显示log,但是也会出现一些特别的情况,直接闪退而且 没有任何预兆. ...

  3. Spring源码剖析依赖注入实现

    Spring源码剖析——依赖注入实现原理 2016年08月06日 09:35:00 阅读数:31760 标签: spring源码bean依赖注入 更多 个人分类: Java   版权声明:本文为博主原 ...

  4. MySQL创建数据库,用户,赋予权限

    CREATE DATABASE 'voyager'; CREATE DATABASE `voyager`; CREATE USER 'dog'@'localhost' IDENTIFIED BY '1 ...

  5. Jenkins忘记管理员密码处理

    1.先找到jenkins安装目录打开config.xml文件. 2.然后编辑,删除以下部分: <useSecurity>true</useSecurity> <autho ...

  6. 如何用 CSS 和 D3 创作旋臂粒子动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xJrOqd 可交互视频 ...

  7. DeepFaceLab: SSE,AVX, OpenCL 等版本说明!

    Deep Fake Lab早期只有两个版本,一个是专门正对NVIDIA显卡的CUDA9的版本,另一个是支持CPU的版本. 三月初该项目作者对tenserFlow,Cuda的版本进行了升级,预编译的软件 ...

  8. java/jsp执行sql语句的方式

    首先给出sql驱动包 引入sql包 import java.sql.*;//java <%@ page import="java.sql.*"%>//jsp 连接mys ...

  9. python多进程并发进程池Pool

    简介: python中的多进程主要使用到 multiprocessing 这个库.低版本python这个库在使用 multiprocessing.Manager().Queue时会出问题,建议大家升级 ...

  10. XML映射文件中关系映射

    映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工表: DROP ...