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, this covers all characters in T, return the empty string ""
.
If There is multiple such windows, you is guaranteed that there would always being only one unique minimum window in S.
Public stringMinwindow (stringSstringt) {if(t==NULL|| t = =""|| s = ="")returnT; if(T.length > S.length)return ""; intMax =s.length+1; varres =""; varHashtable =Newdictionary<Char,int>(); intMin =s.length; for(inti =0;i< t.length;i++) { if(Hashtable. ContainsKey (T[i]) hashtable[t[i]]++; ElseHashtable. ADD (T[i],1); } for(inti =0; i<=s.length-t.length;i++) { varexist =Newdictionary<Char,int>(); intCount =0; for(intj = i;j<s.length;j++) { if(Max < j-i+1) Break; if(Hashtable. ContainsKey (S[j])) {if(exist. ContainsKey (S[j])) {if(Exist[s[j]) <Hashtable[s[j]]) {Count++; EXIST[S[J]]++; if(Count >=t.length) { if(Max > j-i+1) {Res =s.substring (i,j-i+1); max = j-i+1;} Break; } } } Else{Count++; Exist. ADD (S[j],1); if(Count >=t.length) { if(Max > j-i+1) {Res =s.substring (i,j-i+1); max = j-i+1;} Break; } } } } } returnRes; }
Public stringMinwindow (stringSstringt) {if(t==NULL|| t = =""|| s==mull| | s = ="")returnT; if(T.length > S.length)return ""; intMax =s.length+1; varres =""; varHashtable =Newdictionary<Char,int>(); for(inti =0;i< t.length;i++) { if(Hashtable. ContainsKey (T[i]) hashtable[t[i]]++; ElseHashtable. ADD (T[i],1); } intleft =0; intRight =-1; intCount =0; while(right<s.length) { if(Count = =t.length) { if(Max > Right-left +1) {Max= Right-left +1; Res= s.substring (Left,right-left +1); } if(Hashtable. ContainsKey (S[left])) {if(Hashtable[s[left]] = =0) count--; Hashtable[s[left]]++; } Left++; } Else if(right<s.length-1) { Right++; if(Hashtable. ContainsKey (S[right])) {if(hashtable[s[right]]>0) count++; Hashtable[s[right]]--; } } Elseright++; } returnRes; }
Minimum Window Substring