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 ...
随机推荐
- [转载] 你所不知道的TIME_WAIT和CLOSE_WAIT
前言 本文转载自 https://mp.weixin.qq.com/s/FrEfx_Yvv0fkLG97dMSTqw.很久前看到Vincent Bernat在博客中写了一遍关于TCP time-wai ...
- Error after SQL Server 2012 installation: Login Failure for "SQL Server Integration Services 11.0" SSIS service
When you install SQL Server 2012 and you try to connect to SSIS services, you cannot due to that the ...
- iOS 11开发教程(三)运行第一个iOS 11程序
iOS 11开发教程(三)运行第一个iOS 11程序 运行iOS11程序 创建好项目之后,就可以运行这个项目中的程序了.单击运行按钮,如果程序没有任何问题的话,会看到如图1.6和1.7的运行效果. 图 ...
- [BZOJ4567][SCOI2016]背单词(Trie+贪心)
1.题意表述十分难以理解,简单说就是:有n个单词,确定一个背的顺序,使总代价最小. 2.因为第(1)种情况的代价是n*n,这个代价比任何一种不出现第(1)种情况的方案都要大,所以最后肯定不会出现“背某 ...
- HDU 6194 string string string(后缀自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个字符串求其出现恰好k次的子串数量 [题解] 对串建立AC自 ...
- 【POJ】1486:Sorting Slides【二分图关键边判定】
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5390 Accepted: 2095 De ...
- Codeforces Round #298 (Div. 2) A. Exam 构造
A. Exam Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem/A Des ...
- hihocoder #1015 KMP
#include<stdio.h> #include<iostream> #include<math.h> #include<string.h> usi ...
- ThinkPHP中RBAC权限管理的简单应用
RBAC英文全称(Role-Based Access Controller)即基于角色的权限访问控制,简单来讲,一个用户可以拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色-权限”的授 ...
- javascript小记-闭包理解
这几天也在看一些javascript的知识,算是对以往的一个复习,现小记一下,方便以后查询. 相信大家在研究javascript的高级特性的时候,肯定会遇到闭包的概念,自己在各种复习资料中,也发现了不 ...