标签:style blog http color io os ar for sp
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什么的,参考了一些资料http://blog.csdn.net/fightforyourdream/article/details/17373203
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:
底下这个做法看似简单,其实里面各种精巧的设计啊:先找到满足条件的一个window(不一定是最优),每次移动右窗口,吸纳一个新元素进去,如果不是目标元素就跳过continue,如果是的话,hasFound数组对应位置值+1,然后看能不能优化窗口大小 by 看能不能移动左窗口,移动条件就是:始终保证窗口里面含有一个T的所有必要元素(个数要保证),直到移不动为止(再移就无法保证一个完整T的各元素个数了),这时就找到一个新的window,看是否最优。
1 public class Solution { 2 public String minWindow(String S, String T) { 3 int[] hasFound = new int[256]; 4 int[] needtoFind = new int[256]; 5 for (int i=0; i<T.length(); i++) { 6 needtoFind[(int)(T.charAt(i)-‘\0‘)]++; 7 } 8 int count = 0; 9 int start = 0; 10 int end = 0; 11 String minWindow = ""; 12 int minWinSize = Integer.MAX_VALUE; 13 for (; end<S.length(); end++) { 14 if (needtoFind[(int)(S.charAt(end)-‘\0‘)] == 0) continue; 15 char c = S.charAt(end); 16 hasFound[(int)(c-‘\0‘)]++; 17 if (hasFound[(int)(c-‘\0‘)] <= needtoFind[(int)(c-‘\0‘)]) { 18 count++; 19 } 20 if (count == T.length()) { //the current window contains at least T, optimize the window now 21 while (needtoFind[S.charAt(start)]==0 || hasFound[S.charAt(start)]>needtoFind[S.charAt(start)]) { 22 if (hasFound[S.charAt(start)] > needtoFind[S.charAt(start)]) { 23 hasFound[S.charAt(start)]--; 24 } 25 start++; 26 } 27 if (end-start+1 < minWinSize) { 28 minWinSize = end - start + 1; 29 minWindow = S.substring(start, end+1); 30 } 31 } 32 } 33 return minWindow; 34 } 35 }
在处理字符串时候用数组比Hashtable要来的方便, 这道题也可用Hashtable来做,CodeGanker的做法(未深究),基本想法跟上一种解法是一致的
1 public String minWindow(String S, String T) { 2 if(S==null || S.length()==0) 3 return ""; 4 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 5 for(int i=0; i<T.length();i++) 6 { 7 if(map.containsKey(T.charAt(i))) 8 { 9 map.put(T.charAt(i),map.get(T.charAt(i))+1); 10 } 11 else 12 { 13 map.put(T.charAt(i),1); 14 } 15 } 16 int left = 0; 17 int count = 0; 18 int minLen = S.length()+1; 19 int minStart = 0; 20 for(int right=0; right<S.length();right++) 21 { 22 if(map.containsKey(S.charAt(right))) 23 { 24 map.put(S.charAt(right),map.get(S.charAt(right))-1); 25 if(map.get(S.charAt(right))>=0) 26 { 27 count++; 28 } 29 while(count == T.length()) 30 { 31 if(right-left+1<minLen) 32 { 33 minLen = right-left+1; 34 minStart = left; 35 } 36 if(map.containsKey(S.charAt(left))) 37 { 38 map.put(S.charAt(left), map.get(S.charAt(left))+1); 39 if(map.get(S.charAt(left))>0) 40 { 41 count--; 42 } 43 } 44 left++; 45 } 46 } 47 } 48 if(minLen>S.length()) 49 { 50 return ""; 51 } 52 return S.substring(minStart,minStart+minLen); 53 }
Leetcode: Minimum Window Substring
标签:style blog http color io os ar for sp
原文地址:http://www.cnblogs.com/EdwardLiu/p/4007886.html