Minimum Window Substring leetcode java
题目:
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.
题解:
这道题也是用滑动窗口的思想,思想跟 Substring with Concatenation of All Words是一样的,同样是利用HashMap来存Dict,然后来遍历整个母串。因为这里是要求最短的包含子串的字符串,所以中间是可以允许有非子串字符的,当遇见非子串字符而count又没到子串长度时,可以继续走。
当count达到子串长度,说明之前遍历的这些有符合条件的串,用一个pre指针帮忙找,pre指针帮忙找第一个在HashMap中存过的,并且找到后给计数加1后的总计数是大于0的,判断是否为全局最小长度,如果是,更新返回字符串res,更新最小长度,如果不是,继续找。
这道题的代码也参考了code ganker的。
代码如下:
1 public String minWindow(String S, String T) {
2 String res = "";
3 if(S == null || T == null || S.length()==0 || T.length()==0)
4 return res;
5
6 HashMap<Character, Integer> dict = new HashMap<Character, Integer>();
7 for(int i =0;i < T.length(); i++){
8 if(!dict.containsKey(T.charAt(i)))
9 dict.put(T.charAt(i), 1);
else
dict.put(T.charAt(i), dict.get(T.charAt(i))+1);
}
int count = 0;
int pre = 0;
int minLen = S.length()+1;
for(int i=0;i<S.length();i++){
if(dict.containsKey(S.charAt(i))){
dict.put(S.charAt(i),dict.get(S.charAt(i))-1);
if(dict.get(S.charAt(i)) >= 0)
count++;
while(count == T.length()){
if(dict.containsKey(S.charAt(pre))){
dict.put(S.charAt(pre),dict.get(S.charAt(pre))+1);
if(dict.get(S.charAt(pre))>0){
if(minLen>i-pre+1){
res = S.substring(pre,i+1);
minLen = i-pre+1;
}
count--;
}
}
pre++;
}
}//end for if(dict.containsKey(S.charAt(i)))
}
return res;
}
Reference:
http://blog.csdn.net/linhuanmars/article/details/20343903
Minimum Window Substring leetcode java的更多相关文章
- 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! 二.我的解答 先说我的思路: ...
- Java for LeetCode 076 Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [Leetcode][JAVA] Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
随机推荐
- chromedriver与chrome版本对应表,firefox、geckodriver
一. chromedriver与chrome对应表(记得就会更新): chromedriver版本 支持的Chrome版本 v2.36 v64-66 v2.35 v62-64 v2.34 v61-6 ...
- 一、redis系列之基础知识与centos下环境搭建
1. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的ke ...
- 美团外卖iOS App冷启动治理
一.背景 冷启动时长是App性能的重要指标,作为用户体验的第一道“门”,直接决定着用户对App的第一印象.美团外卖iOS客户端从2013年11月开始,历经几十个版本的迭代开发,产品形态不断完善,业务功 ...
- ASP.NET MVC 提高运行速度的几种性能优化方法
主要介绍ASP.NETMVC 应用提速的六种方法,因为没有人喜欢等待,所以介绍几种常用的优化方法. 大家可能会遇到排队等待,遇到红灯要等待,开个网页要等待,等等等. 理所当然,没有人喜欢等待网页慢吞吞 ...
- [python]一个关于默认参数的老问题和一个有关优化的新问题
一个老问题: def func(defau=[]): defau.append(1) return defau print(func())#print[1] print(func())#print[1 ...
- 常见的Javascript报错及解决方案
一.堆栈溢出不顾堆栈中分配的局部数据块大小,向该数据块写入了过多的数据,导致数据越界,以至于覆盖了别的数据.1.哪些操作会引起堆栈溢出?比如递归2.如何解决堆栈溢出?闭包,setTimeout,优化调 ...
- Revit二次开发示例:AutoStamp
该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印. #region Namespaces using System; using System.Collect ...
- wmware虚拟系统光盘的问题
拿到系统盘,需要通过UltralSO工具中:工具-制作光盘映像文件,做成系统iso文件,而不是直接拷贝系统盘里的文件压缩成iso格式. 主要原因:主要是系统盘有一个引导区,win系统复制光盘时,是不能 ...
- hihocoder #1299 : 打折机票 线段树
#1299 : 打折机票 题目连接: http://hihocoder.com/problemset/problem/1299 Description 因为思念新宿的"小姐姐"们, ...
- js面向对象编程-高级内容
JavaScript面向对象 一.为每个对象动态添加属性或方法 功能:返回对象类型原型的引用 prototype的使用 格式:class.prototype 场景: 比如说:A写了一个类,交给B,B在 ...