Topic:
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.
Hide TagsHash Table Pointers String Find the smallest window in the string S, so that the window contains all the characters in the string T, (not in order), where you need to be aware:
- The characters in T can be repeated, and the window needs to be included repeatedly.
- The scii character is used in the test experiment, so you can replace the hash table with a 128-bit array.
- Hash_map cannot be used in Leetcode C + +.
Ideas:
- Use a map int need (hash table or 128-bit array) to store the number of characters in T, because the demand may be negative, so use int.
- Use a mapping bool EXITST (hash table or 128-bit array) to store the existence of characters in T, and use Hashtable to construct this, because find is a traversal lookup.
- Use a var record window to match the number of char in T.
- Using the table start, last Mark window position, start points to the first char,last in the window to the first char (including the end) to the right outside of the window.
- Each time the loop joins or deletes (records a flag) a character, and if it does not exist, the loop continues.
- By judging add to delete flag to operate, update the need table, update Var, if equal to the length of T, update return record.
- Loop end to determine whether to find.
Here's what I wrote, the time complexity is O (length (S)), O (n), and the space complexity is O (length (T)):
1#include <string>2#include 3#include <iostream>4#include <map>5 using namespacestd;6 using namespace__gnu_cxx;7 8 classSolution {9 Public:Ten stringMinwindow (stringSstringT) { One intNumS = S.length (), NUMT =t.length (); A if(nums<1|| numt<1)return ""; - intwinstart=0, winlast=0, Wincount =0, retstart,leng=Int_max; -hash_map<Char,int>need; thehash_map<Char,BOOL>exist; - for(inti =0; i<numt;i++){ -need[t[i]]++; -Exist[t[i]] =true; + } - + BOOLAddorminus; A CharCurchar; at while(winstart<=nums-numt) { - if(wincount!=numt&&winlast<NumS) { -Addorminus =true; -Curchar = s[winlast++]; - } - Else{ inAddorminus =false; -Curchar = s[winstart++]; to } + if(!exist[curchar])Continue; - if(addorminus) { the if(need[curchar]>0) wincount++; *need[curchar]--; $ if(Wincount==numt&&leng>winlast-Winstart) {Panax NotoginsengRetstart =Winstart; -Leng = Winlast-Winstart; the } + } A Else{ the if(wincount==numt&&leng>winlast-winstart+1){ +Retstart = winstart-1; -Leng = winlast-winstart+1; $ } $Need[curchar] + +; - if(need[curchar]>0) wincount--; - } the } - if(leng==Int_max)Wuyi return ""; the returns.substr (Retstart,leng); - } Wu }; - About intMain () $ { - strings ="1a123bac1"; - stringt ="AABC"; - solution Sol; A + stringRET =Sol.minwindow (s,t); thecout<<ret<<Endl; - return 0; $}View Code
Leetcode discussion inside there is another implementation, is also O (n), the implementation of a similar, the difference is that in the loop is to determine the number of characters in the T in the window to add or delete, I write by the target operator character, logically without TA write convenient. Https://oj.leetcode.com/discuss/10337/accepted-o-n-solution
1 classSolution {2 Public:3 stringMinwindow (stringSstringT) {4 if(S.empty () | |t.empty ())5 {6 return "";7 }8 intCount =t.size ();9 intrequire[ -] = {0};Ten BOOLchset[ -] = {false}; One for(inti =0; I < count; ++i) A { -require[t[i]]++; -Chset[t[i]] =true; the } - inti =-1; - intj =0; - intMinlen =Int_max; + intMinidx =0; - while(I < (int) S.size () && J < (int) s.size ()) + { A if(count) at { -i++; -require[s[i]]--; - if(Chset[s[i] && Require[s[i]] >=0) - { -count--; in } - } to Else + { - if(Minlen > I-j +1) the { *Minlen = I-j +1; $Minidx =J;Panax Notoginseng } -require[s[j]]++; the if(Chset[s[j] && require[s[j]] >0) + { Acount++; the } +J + +; - } $ } $ if(Minlen = =Int_max) - { - return ""; the } - returns.substr (Minidx, Minlen);Wuyi } the};View Code
[Leetcode] Minimum Window Substring