不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题。参考了许多资料:
http://leetcode.com/2010/11/finding-minimum-window-in-s-which.html
http://www.geeksforgeeks.org/find-the-smallest-window-in-a-string-containing-all-characters-of-another-string/
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:
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; } }
1 在处理字符串时候用数组比Hashtable要来的方便
2 注意什么时候我们说找到了一个窗口,注意判断条件
import java.util.*; public class Solution { public String minWindow(String S, String T) { if(S.length() == 0 || T.length() == 0) { return ""; } StringBuilder sb = new StringBuilder(); String mins = ""; int min = S.length(); int left = 0, right = 0; int[] tofind = new int[256]; int[] found = new int[256]; for(int i=0; i<T.length(); i++) { tofind[T.charAt(i)]++; } int count = 0; while(right < S.length()) { char rc = S.charAt(right); if(tofind[rc] == 0) { right++; continue; } found[rc]++; if(found[rc] <= tofind[rc]) { // increase count only when it is a useful increase count++; } // found a window, now keep optimizing this window if(count == T.length()) { while(left <= right) { char lc = S.charAt(left); if(tofind[lc] == 0) { // unrelevant case left++; } else if(found[lc] > tofind[lc]) { // can compress left bound of window left++; found[lc]--; } else { break; } } if(right - left + 1 <= min) { // if we have a smaller window size min = right - left; mins = S.substring(left, right+1); } } right++; } return mins; } }