LeetCode 笔记系列16.2 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 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),人生就是一场不停的战斗]的更多相关文章
- 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 ...
- 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 ...
- LeetCode 76. 最小覆盖子串(Minimum Window Substring)
题目描述 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "ABC ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 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 ...
- 【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 ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
随机推荐
- redis配置master-slave模式
由于云服务器存在闪断现象,项目线上会存在基于redis的功能在闪断时段内出现异常,所以redis需要做master-slave模式.直接上代码: 原单机redis,RedisConnectionFac ...
- Maven的个性化定制
用Maven的小伙伴都知道,Maven的宗旨是约定优于配置(Convention Over Configuration). 在宗旨的前提下Maven也提供了个性化定制的Profile,让我们看看使用方 ...
- Bitmap OutOfMemoryError
在创建Bitmap的时候因为对象过多而没有即使回收,导致的内存不足: java.lang.OutOfMemoryError E/AndroidRuntime(21898): at androi ...
- ubuntu apt-get方式安装与卸载
在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...
- atitit.产品console 日志的aticonsole 方案处理总结
atitit.产品console 日志的aticonsole 方案处理总结 1. 主要原理流程 1 2. 调用代码 1 3. 内部主要实现 1 3.1. 放入消息 1 3.2. 读取消息 2 默认可以 ...
- DB2中如何取得随机数
转自:http://blog.csdn.net/jionghan3855/article/details/2246738 在DB2数据库自定义产生指定位数的随机数函数. DB2产生随机数的函数:RAN ...
- tcpdump常用参数说明
(一).学习tcpdump的5个参数 初次使用tcpdump时,使用tcpdump -h命令可以看到它有数十个参数. 根据我们在运维工作中的经验,掌握tcpdump以下5个参数即可满足大部分的工作需要 ...
- 每日英语:Dishing the Dirt on Hand-Washing Guidelines
Americans aren't washing their hands nearly as often and as thoroughly as they should, according to ...
- oracle ORA-12545:因目标主机或对象不存在
解决方法: 1.首先从最基本的入手,这里打开计算机右击,选择管理 2. 找到里面的服务和应用程序,打开服务 3.找到: OracleOraDb11g_home1TNSListener OracleSe ...
- 文件操作之格式化IO
其实在我使用最多的文件操作中,还是喜欢格式化IO控制的方式,简单方便易理解. #include <stdio.h> #include<stdlib.h> int main() ...