[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 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.
使用两个指针start和end去截取S中的字符串,初始都为S头部。end指针先往后遍历,直到T中所有字符都出现在子字符串中或者到S结尾,这时候把start往后挪,尽量使子串长度小,直到再往后挪就会不满足T中字符都在子串中的条件或者到达end,这时候记录start和end并计算子串长度,如果比之前的小则更新start和end。如此反复这两步直到end到S末尾。
问题是两个关键时刻:1.T中所有字符都出现在子字符串中;2. 再往后挪就会不满足T中字符都在子串中的条件,该怎么被捕获。
使用两个HashMap可以实现。第一个HashMap, need记录关于T中所有字符的次数统计,在遍历S中作为标准,不会变化。第二个HashMap, found记录遍历过程中字符出现次数的动态变化。
条件2可以这么实现: 一旦发现found中当前字符(前提是存在于found中)的值已经等于need中的值,则停止start指针的遍历。否则可以继续遍历不过found中的值要-1。
条件1可以联系T的长度来实现:使用变量count记录已在S中找到的T中字符个数。一旦count等于T的长度,开始把start往后挪。那么,什么样的字符是已在S中找到的T中字符,可以算进count?依然使用found和need可以判断,如果found中字符的值小于等于need中它的值,说明这遍历到的字符应该算进count,因为关于这个字符,还没有或者刚刚好满足它应该出现的次数,应该要算作T中字符。而若found中的值大于need中的值,说明该已经遍历到足够多该字符,不需要再来这种字符了,需要的是T中其他的字符,所以不能算进count.
public String minWindow(String S, String T) {
if(T.length()>S.length())
return "";
HashMap<Character, Integer> need = new HashMap<Character, Integer>();
HashMap<Character, Integer> found = new HashMap<Character, Integer>();
for(int i=0;i<T.length();i++) {
char t = T.charAt(i);
if(!need.containsKey(t)) {
need.put(t,1);
found.put(t,0);
} else
need.put(t, need.get(t)+1);
}
int start = 0;
int end = 0;
int minStart = -1;
int minEnd = S.length();
int count = 0;
for(;end<S.length();end++) {
char t = S.charAt(end);
if(need.containsKey(t)) {
found.put(t, found.get(t)+1);
if(found.get(t)<=need.get(t))
count++;
if(count==T.length()) {
for(;start<=end;start++) {
char t2 = S.charAt(start);
if(need.containsKey(t2)) {
if(found.get(t2)>need.get(t2))
found.put(t2, found.get(t2)-1);
else
break;
}
}
if(start>end)
start--;
if(end-start<minEnd-minStart) {
minStart = start;
minEnd = end;
}
}
}
}
return minStart==-1?"":S.substring(minStart,minEnd+1);
}
[Leetcode][JAVA] Minimum Window Substring的更多相关文章
- 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] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]76. 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 (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 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 ...
随机推荐
- iOS手机功能汇总
开发中经常会调用手机功能,今天来汇总一下,若有不足欢迎大家指出,下面分别介绍如下功能 : 电话 短信 邮件 通讯录 定位 跳转应用 跳转App Store 打开其他文件 电话 调用电话有下图两种不同样 ...
- TimeQuest学习
1.物理时钟特性:clock skew(时钟差),jitter(拉动),clock latency(时钟潜伏),这些物理时钟特性又称为uncertainl--非定性,或非理想性. clock skew ...
- javac 命令出现 找不到文件 问题及解决办法
如果环境配置好了,使用java -version回车可以正常查看到版本信息. 使用javac Demo.java 如果提示文件找不到 可能原因1: 源文件与当前命令行不在同目录下,这时候就要切换到同一 ...
- java_method_stringUtils
/** * 字符串英文单双引号处理,将中英文引号转为中文的引号 * @param temp * @return */ public static String getStringDatabase(St ...
- Fedora 21 64位系统安装WPS教程
WPS的Linux版本的出现简直是Linux党的福音,Ubuntu上的WPS安装非常简单,但是在Fedora上却有点小麻烦.主要是库的依赖问题.下面记录一下Fedora 21的64位版安装WPS的完整 ...
- 记录容易忘记的知识点(html 内容)
<xx 表文件名> 导入外部样式表 <link type="text/css" rel="stylesheet" href="xx. ...
- 在ubuntu 16.04系统环境中搭建NAS(samba/iscsi/nfs)
在ubuntu 16.04系统中搭建NAS环境 一.基本配置1:设置静态IPvi /etc/network/interfaces#iface ens32 inet dhcpiface ens32 in ...
- 在Linux上配置xampp后远程访问域名报错
在Linux上配置xampp后远程访问域名报错: New XAMPP security concept: Access to the requested object is only availabl ...
- 【转】Crontab定时任务配置
原文出处:http://www.cnblogs.com/kerrycode/p/3238346.html CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读 ...
- 云端卫士实战录 | Java高级特性之多线程
<实战录>导语 一转眼作为一名Java开发者已经四年多时间了,说长不长说短不短,对于java的感情还是比较深的,主要嘛毕竟它给了我饭吃.哈哈,开个玩笑.今天我想借此机会来和大家聊聊Java ...