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,求S的最小子串(或者最小字符窗口),该子串包含所有T的字符。要求在线性时间内完成。如果没有符合要求的子串,则返回“”。如果有多个满足条件的最小子串,则返回第一个。

解题思路:

  由于题目要求在线性时间内完成,就要减少T的字符的查找时间,故要考虑用hashMap。hashMap的查找时间复杂度是O(1)。由于目标串T可能含有重复字符,因此使用一个哈希表记录目标串的字符和字符出现的次数,同时要记录搜索窗口的已匹配的字符数count,最小字符窗口的开始位置start,已匹配的字符和次数findHashMap.

  当已匹配的总字符数大于目标串的字符数时,即count > tLen时,要尽可能的把窗口的开始位置往后移动,即start前移,使窗口尽可能的小。

  在移动窗口位置时必须满足的条件:

   (1)当前字符不在目标串T里

   (2)已匹配的次数大于目标串里相应字符出现的次数

注意特殊情况的处理

  (1)两个字符串都为空的时候

  (2)S的长度<T的长度

  (3)S字符都不在T里面时start的处理

class Solution {
public:
string minWindow(string S, string T) {
if(S == "" || T == "" ||S.length() < T.length()) return "";
int sLen =S.length(), tLen = T.length();
unordered_map<char,int> hashMap;
for(int i = ; i < tLen; ++ i) hashMap[T[i]]++;
unordered_map<char,int> findHashMap;
int count = , start =, minLen = INT_MAX,minLenStart = -;
for(int i = ; i < sLen; ++ i){
char ch = S[i];
if(hashMap.find(ch)==hashMap.end()) continue;
findHashMap[ch]++;
if(findHashMap[ch] <= hashMap[ch]) count++;
if(count>=tLen){
char c = S[start];
while(findHashMap.find(c) == findHashMap.end() || findHashMap[c] > hashMap[c] ){
if(findHashMap.find(c)!=findHashMap.end()) findHashMap[c]--;
start++;
c = S[start];
}
int len = i-start+;
if(len < minLen){
minLen = len;
minLenStart = start;
}
}
}
if(minLenStart == -)return "";
else return S.substr(minLenStart,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 charact ...

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

    http://www.2fz1.com/post/unity-ugui-recttransform/ //this.transform.position 获取的是世界坐标,而 this.transfo ...

  2. AOP基本名词解释

  3. VTK初学一,动画加AVI录制终于做出来了

      #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRe ...

  4. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?

    什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...

  5. 常用的Mysql数据库操作语句大全

    一.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...

  6. Effective C++ 33 避免遮掩继承而来的名称

    首先介绍一个原则LSP(Liskov Substitution Principle),如果Class D以Public方式继承Class B,则所有B对象可以派上用场的任何地方,D对象一样可以派上用场 ...

  7. php之JavaScript

    JS对于大小写敏感 作用:增加跟html页面的交互性. 应用:验证表单.创建cookies(可插入html页面的编程代码)js插入页面后可由所有现代的浏览器执行.应用于<body>< ...

  8. CSS代码片段【图文】

    1.垂直对齐 .verticalcenter{ position: relative; top: 50%; -webkit-transform: translateY(-50%); -o-transf ...

  9. PHP setcookie() 函数

    语法 setcookie(name,value,expire,path,domain,secure): name 必需.规定 cookie 的名称. value 必需.规定 cookie 的值. ex ...

  10. Json序列化与反序列化

    参考:http://www.cnblogs.com/caofangsheng/p/5687994.html#commentform 下载链接:http://download.csdn.net/deta ...