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 empty string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

思路:问题可以转变为,T中各个字符的数量<= window中的这些字符的数量,所以用map纪录字符与字符数量的关系

  1. class Solution {
  2. public:
  3. bool ifContain(map<char,int>& source, map<char,int>& target) //check if S contains T
  4. {
  5. for(map<char,int>::iterator it = target.begin(); it != target.end(); it++) //map的遍历
  6. {
  7. if(source[it->first] < it->second) return false;
  8. }
  9. return true;
  10. }
  11.  
  12. string minWindow(string S, string T) {
  13. int minLength = INT_MAX;
  14. int start = , end = , minStart = , minEnd = ;
  15. map<char,int> source;
  16. map<char,int> target;
  17. source[S[start]]++; //map的插入[法I]source[key]=value; [法II]source.insert(make_pair(key,value));
  18. for(int i = ; i< T.length(); i++)
  19. {
  20. target[T[i]]++;
  21. }
  22. while()
  23. {
  24. if(ifContain(source, target)){
  25. if(end-start+ < minLength)
  26. {
  27. minStart = start;
  28. minEnd = end;
  29. minLength = end-start+;
  30. if(minLength == T.size()) return S.substr(minStart,minLength);
  31. }
  32. source[S[start]]--; //寻找更小的窗口
  33. start++;
  34. }
  35. else //不包含,则扩大窗口
  36. {
  37. end++;
  38. if(end==S.size()) break;
  39. source[S[end]]++;
  40. }
  41. }
  42. if(minLength>S.size()) return "";
  43. else return S.substr(minStart,minLength);
  44. }
  45. };

76. Minimum Window Substring (String, Map)的更多相关文章

  1. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

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

  3. 76. 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] 76. 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]76. Minimum Window Substring最小字符串窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  6. 76. Minimum Window Substring(hard 双指针)

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

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

  8. 76. Minimum Window Substring (JAVA)

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. [LC] 76. Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

随机推荐

  1. 集成学习之Boosting —— Gradient Boosting原理

    集成学习之Boosting -- AdaBoost原理 集成学习之Boosting -- AdaBoost实现 集成学习之Boosting -- Gradient Boosting原理 集成学习之Bo ...

  2. Linux 网络编程->epoll<-LT/ET模式整理(~相逢何必曾相识~)

    今天自己整理一下epoll,网上有很多经典的介绍,看了很多~收藏了很多~还是整理一下做个积累, 自己的东西好找~ 1. epoll 模型简介 epoll 是Linux I/O 多路复用接口 selec ...

  3. cool 软件 —— Carnac(实时桌面显示按键)

    1. Carnac 下载地址:Carnac, the Magnificent Keyboard Utility 使用说明:carnac – 在屏幕实时显示按键操作

  4. 戴尔 Latiteude E7240 i7-4600U

    一.鲁大师各项数据 二.内存条 三.电池损耗 四.跑分

  5. UVALive 5135 Mining Your Own Bussiness【tarjan点双】

    LINK1 LINK2 题目大意 给你一个无向连通图,让你给一些点染上黑色,需要满足染色之后,断开任意一个节点,要满足任意一个联通块中剩下的节点中至少有一个黑点 思路 一开始想的是把每一个点双联通分量 ...

  6. L3-013 非常弹的球 (30 分)

    刚上高一的森森为了学好物理,买了一个“非常弹”的球.虽然说是非常弹的球,其实也就是一般的弹力球而已.森森玩了一会儿弹力球后突然想到,假如他在地上用力弹球,球最远能弹到多远去呢?他不太会,你能帮他解决吗 ...

  7. 便捷的Jenkins jswidgets

    很多时候我们在构建完成之后需要查看构建的状态,类似github 中的build Status 插件安装 搜索插件 使用 目前好像只支持自由项目的构建 代码集成 <!DOCTYPE html> ...

  8. 使用UltraISO制作U盘启动

    下面给你提供是的一个万能的制作系统U盘的方法,用这个U盘你可以加载任何你想要的系统,即使是Linux系统都是可以,你需要做的就是下载安装软件,下载一个系统安装光盘的镜像文件,然后用软件导入到U盘就可以 ...

  9. C语言:宽字符集操作函数(unicode编码)

    C语言:宽字符集操作函数(unicode编码) 字符分类: 宽字符函数 普通C函数描述 iswalnum() isalnum() 测试字符是否为数字或字母 iswalpha() isalpha() 测 ...

  10. unittest框架出报告乱码的问题解决

    跟着上面的步骤修改好后,unittest断言写法要写成下面这样才能展示非乱码