题目: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。代码如下:

 public String minWindow2(String S, String T){
HashMap<Character, Integer> needToFill = new HashMap<Character, Integer>();
HashMap<Character, LinkedList<Integer>> charAppearenceRecorder = new HashMap<Character, LinkedList<Integer>>();
SortedMap<Integer, Character> winMap = new TreeMap<Integer, Character>();
int minWinStart = -1;
int minWinEnd = S.length();
for(int i = 0; i < T.length(); i++){
if(!needToFill.containsKey(T.charAt(i))){
needToFill.put(T.charAt(i), 1);
charAppearenceRecorder.put(T.charAt(i), new LinkedList<Integer>());
}else {
needToFill.put(T.charAt(i), needToFill.get(T.charAt(i)) + 1);
}
} for(int i = 0; i < S.length(); i++){
char c = S.charAt(i);
if(needToFill.containsKey(c)){
LinkedList<Integer> charList = charAppearenceRecorder.get(c);
if(charList.size() < needToFill.get(c)){
charList.add(i);
winMap.put(i, c);
}else {
//如果某个字母收集过了,需要删除该字母出现的最小的index,保留靠右的部分
int idxToErase = charList.removeFirst();
winMap.remove(idxToErase);
winMap.put(i, c);
charList.add(i);
}
if(winMap.size() == T.length()){
int start = winMap.firstKey();
int end = winMap.lastKey();
if(end - start < minWinEnd - minWinStart){
minWinStart = start;
minWinEnd = end;
}
}
}
} return minWinStart != -1 ? S.substring(minWinStart, minWinEnd + 1) : "";
}

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. C#四舍五入保留一位小数

    DateTime d1 = hrStaff.DateJoin; DateTime d2 = DateTime.Now; TimeSpan d3 = d2.Subtract(d1); ; //int i ...

  2. Python根据系统环境配置日志,Python配置日志 Python logger

    我们通常在写爬虫的时候,需要配置日志,但是有可能是在windows开发的,但是程序的运行环境可是是在Linux中,这时候我们就需要不停的更换日志的目录了 但是我们可以实现通过判断不同的运行环境,来时间 ...

  3. CentOS 安装 升级Firefox

    把旧版本的firefox卸掉: # yum erase firefox 然后安装新版本: # yum firefox

  4. Linux 指令篇:磁盘管理--tree

    Linux 指令篇:磁盘管理--tree 功能说明:以树状图列出目录的内容. 语 法:tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式&g ...

  5. find_if查找vector内对象的成员 作为菜鸟一直不会用也不敢用

    用stl的find方法查找一个包含简单类型的vector中的元素是很简单的,例如 vector<string> strVec; find(strVec.begin(),strVec.end ...

  6. AppModify修改app.config

    public class AppModify { /// <summary> /// 依据连接串名字connectionName返回数据连接字符串 /// </summary> ...

  7. CLoadScene类

    #ifndef __LOADSCENE_H__ #define __LOADSCENE_H__ #include "GameFrameHead.h" class CGameScen ...

  8. 每日英语:Who Ruined The Humanities?

    You've probably heard the baleful reports. The number of college students majoring in the humanities ...

  9. poj3693(后缀数组)

    http://poj.org/problem?id=3693 题意:给出一串字符,需要求这串字符中的最长重复子串,要是有多个,输出字典序最小的......... 我自己的一些想法:这个思路我一开始倒是 ...

  10. [基础]关于extern指针和数组的用法

    之前有在外面面试,遇到一题如下: filea.c char *p = "abcdefg"; fileb.c extern char p[]; printf(]); result=? ...