题目:


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. C#数组简介

    一.数组的定义 数组:是一种包含若干个变量的数据结构,这些变量可以通过索引进行访问. 数组的元素:数组中的变量就称为数组的元素. 元素类型:数组中的元素具有相同的数据类型,该数据类型就称为数组的元素类 ...

  2. MySQL数据库的多种备份与多种还原

    一.备份 1.mysqldump 方法备份 mysqldump备份很简单,格式如下: mysqldump -u用户名 -p密码 数据库名> XX.sql 路径 例如: mysqldump -ur ...

  3. Thinkphp5的安装

    很长没有码代码了,现在开始做这件事情的意义已经完全与以前不一样了.因为最近有相当长的一段休息时间,是个学习的好时间啊.之前接触过TP3.2,听说后来的版本有挺大的改动,因此呢,现在终于有时间可以好好的 ...

  4. jenkins+maven+svn 自动化部署

    背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...

  5. LeetCode1090. 受标签影响的最大值

    问题: 我们有一个项的集合,其中第 i 项的值为 values[i],标签为 labels[i]. 我们从这些项中选出一个子集 S,这样一来: |S| <= num_wanted 对于任意的标签 ...

  6. Hibernate知识梳理

    一.SessionFactory接口 是单个数据库映射关系(ORM)经过编译后的内存镜像.SessionFactory(的实例)作为应用中的一个全局对象(工厂),可以随处打开/创建一个session, ...

  7. 修改const变量

    看下面的一段代码 ; int * j=(int*)(&i); // 运行正确,j确为i的地址,但 int *j=&i; 编译错误 *j=; //确实改变了i的值 printf(&quo ...

  8. 如何从Maven中央存储库下载?

    根据 Apache Maven说明: 下载时由项目的 pom.xml 文件的依赖来决定,目前不在本地存储库触发(当中央存储库包含了一个更新).默认情况下,Maven将从中央存储库下载. 在Maven中 ...

  9. Web - DOM

    1. 简介 DOM(Document Object Mode)是一套web标准,地那一了访问HTML文档的一套属性.方法和事件 其本质: 网页 与 脚本语言 沟通的桥梁 脚本语言通过DOM对象来访问H ...

  10. day03_06 变量详解

    print ("hello world") print("alex") print("jinxing") print("3乘以4= ...