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.
Idea: the title of T may have repeated letters I used a variety of classification discussion results timeout
Just look at the code of the Great God.
classSolution { Public: stringMinwindow (stringSstringT) {intm = S.size (), n =t.size (); if(N <=0|| M < n)return ""; intrequire[ -] = {0}; How many key points are required to record each letter of letters for(inti =0; I < n; ++i) require[t[i]]++; intCount =0; intMinlen = Int_max, Minindex =0; for(ints =0, E =0; e < M; ++e) {require[s[e]]--////end-of-number demand minus 1if(Require[s[e]] >=0) count++; If the demand is greater than or equal to 0, the new number is matched. while(Count = =N) {//All letters are matchedif(E-s +1<Minlen) {//length is smaller to record the new length and start position Minlen= E-s +1; Minindex=s; } Require[s[s]]++;//Start position backward Move update demandif(Require[s[s]) >0) count--; S++; } } if(Minlen = = Int_max)return ""; returns.substr (Minindex, Minlen);};
"Leetcode" Minimum window Substring (hard) ★