Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题。参考了许多资料:
http://leetcode.com/2010/11/finding-minimum-window-in-s-which.html
http://tianrunhe.wordpress.com/2013/03/23/minimum-window-substring/
最有用的最后一个资料,因为里面有一个例子详细说明了如何变化,我加上了一些中文备注:
For example,
S = “ADOBECODEBANC”
T = “ABC”
Minimum window is “BANC”.
Thoughts:
The idea is from here. I try to rephrase it a little bit here. The general idea is that we find a window first, not necessarily the minimum, but it’s the first one we could find, traveling from the beginning of S. We could easily do this by keeping a count of the target characters we have found. After finding an candidate solution, we try to optimize it. We do this by going forward in S and trying to see if we could replace the first character of our candidate. If we find one, we then find a new candidate and we update our knowledge about the minimum. We keep doing this until we reach the end of S. For the giving example:
- We start with our very first window: “ADOBEC”, windowSize = 6. We now have “A”:1, “B”:1, “C”:1 (保存在needToFind数组里)
- We skip the following character “ODE” since none of them is in our target T. We then see another “B” so we update “B”:2. Our candidate solution starts with an “A” so getting another “B” cannot make us a “trade”. (体现在代码就是只有满足hasFound[S.charAt(start)] > needToFind[S.charAt(start)]) 才能移动左指针start)
- We then see another “A” so we update “A”:2. Now we have two “A”s and we know we only need 1. If we keep the new position of this “A” and disregard the old one, we could move forward of our starting position of window. We move from A->D->O->B. Can we keep moving? Yes, since we know we have 2 “B”s so we can also disregard this one. So keep moving until we hit “C”: we only have 1 “C” so we have to stop. Therefore, we have a new candidate solution, “CODEBA”. Our new map is updated to “A”:1, “B”:1, “C”:1.
- We skip the next “N” (这里忽略所有不在T的字符:用needToFind[S.charAt(start)] == 0来判断) and we arrive at “C”. Now we have two “C”s so we can move forward the starting position of last candidate: we move along this path C->O->D->E until we hit “B”. We only have one “B” so we have to stop. We have yet another new candidate, “BANC”.
- We have hit the end of S so we just output our best candidate, which is “BANC”.
package Level4; /**
* 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. Discuss *
*/
public class S76 { public static void main(String[] args) {
} public String minWindow(String S, String T) {
// 因为处理的是字符,所以可以利用ASCII字符来保存
int[] needToFind = new int[256]; // 保存T中需要查找字符的个数,该数组一旦初始化完毕就不再改动
int[] hasFound = new int[256]; // 保存S中已经找到字符的个数,该数组会动态变化 for(int i=0; i<T.length(); i++){ // 初始化needToFind为需要查找字符的个数,
needToFind[T.charAt(i)]++; // 如例子中T为ABC,则将会被初始化为:needToFind[65]=1, nTF[66]=2, nTF[67]=3
} int count = 0; // 用于检测第一个符合T的S的字串
int minWindowSize = Integer.MAX_VALUE; // 最小窗口大小
int start = 0, end = 0; // 窗口的开始喝结束指针
String window = ""; // 最小窗口对应的字串 for(; end<S.length(); end++){ // 用end来遍历S字符串
if(needToFind[S.charAt(end)] == 0){ // 表示可以忽略的字符,即除了T(ABC)外的所有字符
continue;
}
char c = S.charAt(end);
hasFound[c]++; // 找到一个需要找的字符 if(hasFound[c] <= needToFind[c]){ // 如果找到的已经超过了需要的,就没必要继续增加count
count++;
}
if(count == T.length()){ // 该窗口中至少包含了T
while(needToFind[S.charAt(start)] == 0 || // 压缩窗口,往后移start指针,一种情况是start指针指的都是可忽略的字符
hasFound[S.charAt(start)] > needToFind[S.charAt(start)]){ // 另一种情况是已经找到字符的个数超过了需要找的个数,因此可以舍弃掉多余的部分
if(hasFound[S.charAt(start)] > needToFind[S.charAt(start)]){
hasFound[S.charAt(start)]--; // 舍弃掉多余的部分
}
start++; // 压缩窗口
} if(end-start+1 < minWindowSize){ // 保存最小窗口
minWindowSize = end-start+1;
window = S.substring(start, end+1);
}
}
}
return window;
}
}
Minimum Window Substring @LeetCode的更多相关文章
- Minimum Window Substring leetcode java
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- 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! 二.我的解答 先说我的思路: ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)
https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, s, ...
- [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 ...
随机推荐
- js正则语法
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数 ...
- Hadoop MultipleOutputs 结果输出到多个文件夹 出现数据不全,部分文件为空
如题:出现下图中的情况(设置reduceNum=5) 感觉很奇怪,排除了很久,终于发现是一个第二次犯的错误:丢了这句 this.mOutputs.close(); 加上这句,一切恢复正常!
- codeforces 613A. Peter and Snow Blower
题目链接 给一个多边形, 一个多边形外的定点, 求这个点距离多边形的最短距离和最长距离. 最长距离肯定是和某个顶点的连线, 而最短距离是和点的连线或是和某条边的连线. 对于一条边上的两个点a, b, ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- 自定义Chrome 背景色 (FF需要User Styles插件)
Chrome有个自定义背景色的文件 Custom.css 默认里面什么字都没写 html, body {background-color: #e0dcc0!important;} 这个颜色 ...
- ECMAScript 5中新增的数组方法
ECMAScript 5中定义了9个新的数组方法,用于遍历.映射.过滤.检测.简化和搜索数组. 在开始介绍之前,很有必要对这几个新增的数组方法做一个概述.首先,大多数方法的第一个参数接收一个函数,并且 ...
- Linux软件安装包中devel与非devel包之间的区别
带devel(develop)的包,俗称开发包.功能上与普通包相同,但体积更大使用rpm -qi看看这两类包的区别: # rpm -qi glibc-devel-2.12-1.149.el6.x86_ ...
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...
- 精通Activity
在平时开发中,Activity我们每个人应用的都滚瓜烂熟,回忆起来没有太难的地方,但是我们学习知识不应该只知其一不知其二,这样才能在学习的道理上越走越远,今天我要给大家分享的内容会让大家明白一些And ...
- iOS开发之第三方登录微信-- 史上最全最新第三方登录微信方式实现
项目地址 : https://github.com/zhonggaorong/weixinLoginDemo 最新版本的微信登录实现步骤实现: 1.在进行微信OAuth2.0授权登录接入之前,在 ...