https://leetcode.com/problems/substring-with-concatenation-of-all-words/
You is given a string, s, and a list of words, words, that is all of the same length. Find all starting indices of substring (s) in s that's a concatenation of each word in words exactly onc E and without any intervening characters.
For example, given:
s:"barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(Order does not matter).
classSolution { Public: BOOLFuncstringS, vector<string>& words, map<string,int>&h) {intn =words.size (); inteach = words[0].length (); Map<string,int>MH; mh.clear (); inti =0; for(; I<=s.length ()-each; i+=Each ) { stringSub =S.substr (i, each); if(H.find (sub)! =H.end ()) { ++Mh[sub]; if(Mh[sub] > H[sub])return false; } Else return false; } return true; } Vector<int> findsubstring (stringS, vector<string>&words) {Vector<int>Res; intn =words.size (); if(n = =0)returnRes; Map<string,int>h; for(intI=0; i<n; ++i) {if(H.find (words[i]) = =H.end ()) {H.insert (Make_pair (words[i),1)); } Else{H[words[i]]++; } } inteach = words[0].length (); inttot = each *N; if(S.length () < tot)returnRes; for(inti =0; I <= s.length ()-tot; ++i) {stringSub =s.substr (i, tot); //cout << Sub << Endl; if(func (Sub, words, h)) res.push_back (i); } returnRes; }};
View Code
https://leetcode.com/problems/minimum-window-substring/
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.
classSolution { Public: stringMinwindow (stringSstringt) {if(s.length () = =0|| S.length () < T.length ())return ""; if(s.length () = =t.length ()) { if(s = = t)returns; } Map<Char,int>h; h.clear (); Map<Char,BOOL>MH; mh.clear (); for(intI=0; I<t.length (); ++i) {H[t[i]]++; Mh[t[i]]=true; } intCNT = T.length (), L =0, Minrange =Int_max, Mini, Minj; for(intR=0; R<s.length (); ++r) {if(Mh[s[r]]) {--H[s[r]]; if(H[s[r]] >=0) --CNT; } if(CNT = =0) { while(!mh[s[l] | | h[s[l]] <0) { ++H[s[l]]; ++l; } if(Minrange > R-l +1) {Minrange= R-l +1; Mini=l; } } } if(Minrange = = Int_max)return ""; returns.substr (Mini, Minrange); }};
View Code
[email protected] [30/76] Substring with concatenation of all Words & Minimum Window Substring (Hashtable, both pointers)