您的位置:首页 > 其它

Leetcode: Minimum Window Substring

2014-10-06 01:45 357 查看
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.


难度:90 String问题里面有很多不好做的,动不动就DP什么的,参考了一些资料/article/1340700.html

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”.

底下这个做法看似简单,其实里面各种精巧的设计啊:先找到满足条件的一个window(不一定是最优),每次移动右窗口,吸纳一个新元素进去,如果不是目标元素就跳过continue,如果是的话,hasFound数组对应位置值+1,然后看能不能优化窗口大小 by 看能不能移动左窗口,移动条件就是:始终保证窗口里面含有一个T的所有必要元素(个数要保证),直到移不动为止(再移就无法保证一个完整T的各元素个数了),这时就找到一个新的window,看是否最优。

public class Solution {
public String minWindow(String S, String T) {
int[] hasFound = new int[256];
int[] needtoFind = new int[256];
for (int i=0; i<T.length(); i++) {
needtoFind[(int)(T.charAt(i)-'\0')]++;
}
int count = 0;
int start = 0;
int end = 0;
String minWindow = "";
int minWinSize = Integer.MAX_VALUE;
for (; end<S.length(); end++) {
if (needtoFind[(int)(S.charAt(end)-'\0')] == 0) continue;
char c = S.charAt(end);
hasFound[(int)(c-'\0')]++;
if (hasFound[(int)(c-'\0')] <= needtoFind[(int)(c-'\0')]) {
count++;
}
if (count == T.length()) { //the current window contains at least T, optimize the window now
while (needtoFind[S.charAt(start)]==0 || 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 < minWinSize) {
minWinSize = end - start + 1;
minWindow = S.substring(start, end+1);
}
}
}
return minWindow;
}
}


在处理字符串时候用数组比Hashtable要来的方便, 当然这道题也可用HashMap来做:

public class Solution {
public String minWindow(String S, String T) {
if (S==null || T==null || S.length()<T.length()) return "";
HashMap<Character, Integer> target = new HashMap<Character, Integer>();
HashMap<Character, Integer> real = new HashMap<Character, Integer>();
for (int m=0; m<T.length(); m++) {
char c = T.charAt(m);
if (target.containsKey(c)) {
int times = target.get(c);
target.put(c, times+1);
}
else target.put(c, 1);
}
int l = 0;
int left = 0;
int minWsize = Integer.MAX_VALUE;
String minWin = "";
for (int i=0; i<S.length(); i++) {
char a = S.charAt(i);
if (target.containsKey(a)) {
if (real.containsKey(a)) {
int times = real.get(a);
real.put(a, times+1);
}
else real.put(a, 1);
if (real.get(a) <= target.get(a)) l++;
}
else continue;
if (l == T.length()) { //found the first suitable window, try to optimize the window by moving left edge
while (left <= i) {
char temp = S.charAt(left);
if (!target.containsKey(temp)) {
left++;
continue;
}
if (real.get(temp) <= target.get(temp)) break;
else {
real.put(temp, real.get(temp)-1);
left++;
}
}
if (minWsize > i-left+1) {
minWin = S.substring(left, i+1);
minWsize = i-left+1;
}
}
}
return minWin;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: