"Leetcode" Minimum window Substring (hard) ★

Source: Internet
Author: User

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) ★

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.