leetcode@ [30/76] Substring with concatenation of all Words & Minimum Window Substring (Hashtable, both pointers)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.