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.
1 public class Solution { 2 public String minWindow(String S, String T) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 if(S == null || S.length() == 0 || T == null || T.length() == 0) return ""; 6 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 7 int head = 0, tail = 0; 8 int min = S.length() + 1; 9 String result = ""; 10 int appeared = 0; 11 HashMap<Integer, Integer> t = new HashMap<Integer, Integer>(); 12 for(int i = 0; i < T.length(); i ++){ 13 int c = (int)T.charAt(i); 14 if(!t.containsKey(c)) t.put(c, 1); 15 else t.put(c, t.get(c) + 1); 16 } 17 for(tail = 0; tail < S.length(); tail ++){ 18 int indexx = (int)S.charAt(tail); 19 if(t.containsKey(indexx)){ 20 if(map.containsKey(indexx)){ 21 map.put(indexx, map.get(indexx) + 1); 22 if(map.get(indexx) <= t.get(indexx)) appeared ++; 23 }else{ 24 appeared ++; 25 map.put(indexx, 1); 26 } 27 if(appeared == T.length()){ 28 while(head <= tail){ 29 int index = (int)S.charAt(head); 30 if(t.containsKey(index)){ 31 if(map.get(index) == t.get(index)){ 32 map.put(index, map.get(index) - 1); 33 appeared --; 34 int cur = tail - head; 35 if(min > cur){ 36 min = cur; 37 result = S.substring(head, tail + 1); 38 } 39 head ++; 40 break; 41 }else if(map.get(index) > t.get(index)){ 42 map.put(index, map.get(index) - 1); 43 } 44 } 45 head ++; 46 } 47 } 48 } 49 } 50 return result; 51 } 52 }
也可以在循环的时候只存左右两个index,而不存string, 以speed up。
1 public String minWindow(String S, String T) { 2 // Start typing your Java solution below 3 // DO NOT write main() function 4 if(S==null || T==null) return ""; 5 Map<Character,Integer> total = new HashMap<Character,Integer>(); 6 for(int i=0;i<T.length();i++){ 7 char c = T.charAt(i); 8 if(total.containsKey(c)) 9 total.put(c,total.get(c)+1); 10 else 11 total.put(c,1); 12 } 13 14 Map<Character,Integer> covered = new HashMap<Character,Integer>(); 15 int start=0, 16 end=-1, 17 cur1=0, 18 cur2=0, 19 count=0; 20 21 while(cur1<S.length() && cur2<S.length()){ 22 while(cur1<S.length() && !total.containsKey(S.charAt(cur1))) 23 cur1++; 24 25 if(cur1<S.length()){ 26 if(cur2<cur1) cur2=cur1; 27 28 if(count<T.length()){ 29 while(cur2<S.length() && !total.containsKey(S.charAt(cur2))) 30 cur2++; 31 if(cur2<S.length()){ 32 char c = S.charAt(cur2); 33 if(!covered.containsKey(c) ){ 34 covered.put(c,1); 35 count++; 36 }else{ 37 if(covered.get(c)<total.get(c)) 38 count++; 39 covered.put(c,covered.get(c)+1); 40 } 41 if(count<T.length()) cur2++; 42 } 43 }else{ 44 if(start>end || end-start>cur2-cur1){ 45 end=cur2; 46 start=cur1; 47 } 48 char c = S.charAt(cur1); 49 covered.put(c,covered.get(c)-1); 50 cur1++; 51 if(covered.get(c)<total.get(c)){ 52 count--; 53 cur2++; 54 } 55 } 56 } 57 } 58 return S.substring(start,end+1); 59 }
1 class Solution { 2 public: 3 string minWindow(string S, string T) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int markT[300]; 7 int count[300]; 8 for (int i=0; i<300; i++) 9 markT[i] = count[i] = 0; 10 int totnum=0, curnum=0; 11 for (int i=0; i<T.length(); i++){ 12 if ( markT[T[i]]==0 ) totnum++; 13 markT[T[i]]++; 14 } 15 string ret=""; 16 for (int i=0, j=0; j<S.length(); j++ ){ 17 if ( markT[S[j]] ){ 18 count[S[j]]++; 19 if ( count[S[j]]==markT[S[j]] ) curnum++; 20 } 21 while ( i<=j && curnum==totnum ){ 22 if ( ret=="" || j-i+1<ret.length() ) 23 ret = S.substr( i, j-i+1 ); 24 if ( markT[S[i]] ){ 25 if ( count[S[i]]==markT[S[i]] ) curnum--; 26 count[S[i]]--; 27 } 28 i++; 29 } 30 } 31 return ret; 32 } 33 };
posted on 2013-11-07 10:09 Step-BY-Step 阅读(208) 评论(0) 编辑 收藏 举报