题目: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.

上一个系列我们讲到了O(N*M)的解法,这里主要的矛盾是,当遇到一个合法的window的时候,我们无法找到高效的办法(最好是O(1))来找到这个window的起点。下面的O(NlogM)的解法在上一个解法的基础上,使用一个SoredMap来存放当前的window。

该map的key是S的index,value对应S中该index的字母。因为是SortedMap,在发现合法的Window后,总是能通过firstKey和lastKey得到window的长度。而且也免除了使用额外的bit_status来检验合法window的需要。

同样的,当一个字母收集超过了T中要求的数目,那么删除charAppearenceRecorder中对应链表的头节点,同时,还需删除SortedMap中该index为key的entry。代码如下:

  1. public String minWindow2(String S, String T){
  2. HashMap<Character, Integer> needToFill = new HashMap<Character, Integer>();
  3. HashMap<Character, LinkedList<Integer>> charAppearenceRecorder = new HashMap<Character, LinkedList<Integer>>();
  4. SortedMap<Integer, Character> winMap = new TreeMap<Integer, Character>();
  5. int minWinStart = -1;
  6. int minWinEnd = S.length();
  7. for(int i = 0; i < T.length(); i++){
  8. if(!needToFill.containsKey(T.charAt(i))){
  9. needToFill.put(T.charAt(i), 1);
  10. charAppearenceRecorder.put(T.charAt(i), new LinkedList<Integer>());
  11. }else {
  12. needToFill.put(T.charAt(i), needToFill.get(T.charAt(i)) + 1);
  13. }
  14. }
  15.  
  16. for(int i = 0; i < S.length(); i++){
  17. char c = S.charAt(i);
  18. if(needToFill.containsKey(c)){
  19. LinkedList<Integer> charList = charAppearenceRecorder.get(c);
  20. if(charList.size() < needToFill.get(c)){
  21. charList.add(i);
  22. winMap.put(i, c);
  23. }else {
  24. //如果某个字母收集过了,需要删除该字母出现的最小的index,保留靠右的部分
  25. int idxToErase = charList.removeFirst();
  26. winMap.remove(idxToErase);
  27. winMap.put(i, c);
  28. charList.add(i);
  29. }
  30. if(winMap.size() == T.length()){
  31. int start = winMap.firstKey();
  32. int end = winMap.lastKey();
  33. if(end - start < minWinEnd - minWinStart){
  34. minWinStart = start;
  35. minWinEnd = end;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. return minWinStart != -1 ? S.substring(minWinStart, minWinEnd + 1) : "";
  42. }

O(NlogM)

代码的流程很符合O(N*M)的方法。就不“举一个栗子”了吧。

总结下:

1.合理运用embeded的数据结构。

LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]的更多相关文章

  1. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  2. LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  3. LeetCode 76. 最小覆盖子串(Minimum Window Substring)

    题目描述 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "ABC ...

  4. Minimum Window Substring @LeetCode

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

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

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

  7. 53. Minimum Window Substring

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

  8. leetcode76. Minimum Window Substring

    leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...

  9. 刷题76. Minimum Window Substring

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

随机推荐

  1. redis配置master-slave模式

    由于云服务器存在闪断现象,项目线上会存在基于redis的功能在闪断时段内出现异常,所以redis需要做master-slave模式.直接上代码: 原单机redis,RedisConnectionFac ...

  2. Maven的个性化定制

    用Maven的小伙伴都知道,Maven的宗旨是约定优于配置(Convention Over Configuration). 在宗旨的前提下Maven也提供了个性化定制的Profile,让我们看看使用方 ...

  3. Bitmap OutOfMemoryError

    在创建Bitmap的时候因为对象过多而没有即使回收,导致的内存不足: java.lang.OutOfMemoryError E/AndroidRuntime(21898):     at androi ...

  4. ubuntu apt-get方式安装与卸载

    在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...

  5. atitit.产品console 日志的aticonsole 方案处理总结

    atitit.产品console 日志的aticonsole 方案处理总结 1. 主要原理流程 1 2. 调用代码 1 3. 内部主要实现 1 3.1. 放入消息 1 3.2. 读取消息 2 默认可以 ...

  6. DB2中如何取得随机数

    转自:http://blog.csdn.net/jionghan3855/article/details/2246738 在DB2数据库自定义产生指定位数的随机数函数. DB2产生随机数的函数:RAN ...

  7. tcpdump常用参数说明

    (一).学习tcpdump的5个参数 初次使用tcpdump时,使用tcpdump -h命令可以看到它有数十个参数. 根据我们在运维工作中的经验,掌握tcpdump以下5个参数即可满足大部分的工作需要 ...

  8. 每日英语:Dishing the Dirt on Hand-Washing Guidelines

    Americans aren't washing their hands nearly as often and as thoroughly as they should, according to ...

  9. oracle ORA-12545:因目标主机或对象不存在

    解决方法: 1.首先从最基本的入手,这里打开计算机右击,选择管理 2. 找到里面的服务和应用程序,打开服务 3.找到: OracleOraDb11g_home1TNSListener OracleSe ...

  10. 文件操作之格式化IO

    其实在我使用最多的文件操作中,还是喜欢格式化IO控制的方式,简单方便易理解. #include <stdio.h> #include<stdlib.h> int main() ...