题目:


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语言输出多位小数

    #include<stdio.h>#include<stdlib.h>int main(){int i=0;int m=19;int n=3;int s=0;s=m/n;pri ...

  2. jquery图片切换插件jquery.cycle.js参数详解

    转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...

  3. usb hub 设备流程图

    在此处负责而来:http://blog.csdn.net/xuelin273/article/details/38646851  下面的转载于:http://blog.csdn.net/qianguo ...

  4. rs485多主

    因复位时I/O口都输出高电平.如果把I/O口直接与RS-485接口芯片的驱动器使能端DE端相连,会在CPU复位其间DE为高,从而使本节点处于发送状态.如果此时总线上其它节点在发送数据,则此次数据传输将 ...

  5. nrf52裸机学习——GPIO操作

    /** * @brief Function for writing a value to a GPIO pin. * * Note that the pin must be configured as ...

  6. 笔记-python-实用-程序运算时间计算

    方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.no ...

  7. cygwin的使用

    参考资料: 对话 UNIX: 在 Windows 上使用 Cygwin  Cygwin使用指南

  8. Activity树图

  9. MongoDB学习-->设置通用的自增ID替代ObjectId

    插入mongodb数据时,会为其分配一个随机id,想要设置通用的自增id,可以进行以下操作 1.创建自增序列 package com.tangzhe.autoid; import lombok.Dat ...

  10. Python 实战一

    列表ID的显示 起初ID显示的是数据库中的id,因为数据库中的id是自增长的,所以删除一条后,这里显示就叉开了,这里使用索引的方式来显示. 这个功能实现的逻辑: 第一:定义一个表格的架构,用id=‘i ...