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 ...
随机推荐
- 变量覆盖漏洞学习及在webshell中的运用
一.发生条件: 函数使用不当($$.extract().parse_str().import_request_variables()等) 开启全局变量 二.基础了解: 1.$$定义 $$代表可变变量, ...
- shell script执行的几种方式
编写一个shell脚本test.sh,内容如下 a='测试执行方式' echo $a 方式1 使用路径的方式执行 chmod a+x test.sh ./test.sh 执行结果如下 当脚本执行之后, ...
- C++实现平衡二叉树
1.概念 平衡二叉树(AVL Tree)首先要满足二叉树的定义,如下 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 若左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若右子树不空, ...
- beeshell —— 开源的 React Native 组件库
介绍 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...
- iOS 11开发教程(一)
iOS 11开发概述 iOS 11是目前苹果公司用于苹果手机和苹果平板电脑的最新的操作系统.该操作系统的测试版于2017年6月6号(北京时间)被发布.本章将主要讲解iOS 11的新特性.以及使用Xco ...
- HTML5区块和大纲算法
原文链接: Using HTML sections and outlines - Mozilla Developer Network 每集HTML5+CSS3网页布局教程-2大纲算法 HTML5标准带 ...
- [HDU6198]number number number
题目大意: 给定一个数k表示你可以从包括0的斐波那契数列中任取k个数,得到它们的和.求最小的不能得到的自然数. 思路: 打表找规律,可以发现答案为f(2k+3)-1,然后用公式f(i)=f(i/2)* ...
- BZOJ 4197 NOI 2015 寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...
- BZOJ 1192 鬼谷子的钱袋 数论
1192:鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1926 Solved: 1417 题目连接 http://www.lydsy ...
- MySQL命令行工具各功能说明(转)
MySQL 服务器端使用工具程序 mysqld - SQL 后台程序(即 MySQL 服务器进程).该程序必须启动运行,才能连接服务器来访问数据库. mysqld_safe - 服务器启动脚本,可以通 ...