"Minimum window" cpp

Source: Internet
Author: User

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.

Code:

classSolution { Public:    stringMinwindow (stringSstringt) {if(S.empty () && t.empty ())return ""; if(S.size () <t.size ())return ""; Const intAscii_max = the; //record how much times a char occurs in T            intT_char_count[ascii_max] = {0};  for(intI=0; I<t.size (); ++i) {t_char_count[(int) t[i]]++; } //record how much times a char occurs in S            intS_char_count[ascii_max] = {0}; //Global min Begin and end index for minimum interval            intbegin_min=-1, end_min=s.size (); //Local min Begin (no need to record local min end, because it is ' I ')            intBegin =0; intMatch_size =0;  for(intI=0; I<s.size (); ++i) {//Current interval not match && current char in T                if(T_char_count[(int) s[i]]>0 )                {                    //cout << s[i] << ":" << T_char_count[s[i]] << Endl;s_char_count[(int) s[i]]++; //If a char occurs more times in current interval s than in T, can not increase match_size                    if(S_char_count[(int) s[i]]<=t_char_count[(int) S[i]) match_size++; }                if(match_size==t.size ()) {                    //move begin forward untill not match                     while(begin<=i) {//address chars not in t                        if(S_char_count[(int) s[begin]]>0 )                        {                            if(S_char_count[(int) s[begin]]-1<t_char_count[(int) S[begin]) {//cout << S_char_count[s[begin]] << Endl;match_size--;  Break; } s_char_count[(int) s[begin]]--; } Begin++; } s_char_count[(int) s[begin]]--; //update global min begin and End                    if(End_min-begin_min > I-begin) {end_min = i; begin_min =begin;} Begin++; }            }            if(End_min-begin_min>s.size ())return ""; returnS.substr (begin_min,end_min-begin_min+1); }};

Tips

Once thought this kind of question relatively simple answer, actually does not have the algorithm template routine question is the most bothered.

The idea at the beginning of this question is to record the left and right side of each character in T, and so on;

In the case of Bruce Force's violent solution, the time complexity can be O (n²):

1. Traverse each location, centered on each position, and go left and right until it contains all the elements, whichever is the shortest.

2. Take the shortest of all interval.

The solution for AC can be thought of as follows:

1. Maintain several core variables:

A) Each character in T appears several times (T_char_count)

b) within the current range of s, each character in T appears several times (S_char_count)

c) s current interval satisfies the inclusion of T (Match_size)

2. The idea of this problem is very interesting: first find the area containing T, once you find such an interval, then narrow the interval.

The method of reducing the interval: the trailing pointer remains motionless, and the front pointer moves backwards until Match_size < T.size (), proving that the current interval is the smallest one to satisfy the condition.

3. Update the minimum interval dynamically.

Main references:

Http://www.cnblogs.com/TenosDoIt/p/3461301.html

Http://fisherlei.blogspot.sg/2012/12/leetcode-minimum-window-substring.html

"Minimum window" cpp

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.