Title: (HashTable-A-point String)
Given a string S and a string T, find the minimum window in S which would 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 so covers all characters in T, return the emtpy string "" .
If There is multiple such windows, you is guaranteed that there would always being only one unique minimum window in S.
Exercises
There is a thought called sliding window: Reference http://www.cnblogs.com/springfor/p/3872559.html
Public classSolution { Publicstring Minwindow (String S, String T) {if(s.length () = =0|| T.length () = =0|| s==NULL|| t==NULL) return ""; HashMap<Character,Integer> map =NewHashMap <Character,Integer>(); for(intI=0; I<t.length (); i++) if(Map.containskey (T.charat (i))) Map.put (T.charat (i), map.Get(T.charat (i)) +1); ElseMap.put (T.charat (i),1); intCount=0; intPre=0; String Res=""; intMin=s.length () +1;//equal = S.length () just set a initial value for(intI=0; I<s.length (); i++) { if(Map.containskey (S.charat (i))) {Map.put (S.charat (i)), map.Get(S.charat (i))-1); if(Map.Get(S.charat (i)) >=0) Count++; while(count==t.length ()) { if(Map.containskey (S.charat (PRE))) {Map.put (S.charat ) (pre), map.Get(S.charat (PRE)) +1); if(Map.Get(S.charat (PRE)) >0) { if(min>i-pre+1) {min=i-pre+1; Res=s.substring (pre,i+1); } Count--; }} Pre++; } } } returnRes; }}
[Leetcode] Minimum WIndow