Minimum Window substring Topic
Analytical
//76. Minimum Window Substringclasssolution_76 { Public://1) begin points to 0, end moves back until the Begin-end interval contains all the characters in T. Record window length d //2) and then begin to remove the element until the removed character is the character in T is stopped, and a character in T is not included in the window, //3) continues to move end until all the characters in T are included in the window, re-record the smallest window d. //4) So loop to know the last character in the end to S. //Time complexity of O (n)String Minwindow_ (string s, String t) {string result;if(S.empty () | | S.size () <t.size ()) {returnResult } unordered_map<Char,int> MP;//store characters in T for easy matching with s intleft =0;intCNT =0;//Window string to count intMinlen = S.size ()+1; for(inti =0; I < t.size (); i++) {mp[t[i]]++; } for(intright =0; Right < S.size (); right++) {if(Mp.find (S[right])!=mp.end ())//t characters in left~right window{if(mp[s[right]]>0)//Experience the role of >0: when there are repeating strings in S, the first match{cnt++;//Counter +1} mp[s[right]]--;//When there are duplicate elements, it may be reduced to negative while(Cnt==t.size ())////When all the characters of T are inside the window, start moving the left window{if(Mp.find (S[left])!=mp.end ()) {if(Minlen>right-left+1) {Minlen = Right-left +1; result = S.substr (left, Right-left +1); } mp[s[left]]++;//include it in MP and continue to find if(mp[s[left]]>0) {cnt--; }} left++;//Right Shift window to the left} } }returnResult } string Minwindow (string s, String t) {string s, T;returnMinwindow_ (S, T); }};
Source of the topic
Minimum Window Substring