Leetcode First brush _minimum Window Substring

Source: Internet
Author: User

Good question, string, linear time.

I think the first person to get this question should not know how to do it, or I am too weak. Let's find out what the problem requires. From a long string, the characters in the string contain exactly the characters in the other given target string, and the string is minimal in length. There is also a very important simplification, the problem pointed out the requirement of the shortest string only one, this restriction actually implies the whole idea of the problem, just find a long string, and then shrink to not be reduced.

From the requirements of the topic can be found that the problem for the sequence of characters in the string is not required, so it is natural to think of using a hash table to save the number of each character in the target string, and then find a string in the source strings, contains more than the number of characters equal to the target string. Difficult is difficult in how to achieve this function.

The number of each character in the target string can be used as its requirement, whenever a character is found in a long string, the character exists in the target string, then the demand should be reduced by 1, when the requirement equals 0, it means that the characters in the long string can meet the demand of the character in the target string so far. If the requirement of a target string character becomes negative, the supply of this character in the long string is over the current length. For the sake of simplicity, I define a long string to provide this character when the demand for the character is positive for the target string, and to satisfy the rigid demand, the supply is superfluous. The next question is, how do you know that the target string is fully satisfied? Of course you can go to the individual scan requirements are not all become non-positive, but there is a more simple method, that is, the length of the target string as a total demand, when the demand for rigidity, the total demand minus 1, when the total demand becomes 0 o'clock, the target string is satisfied.

The process described above found a string in the long strings, can fully satisfy the target string, but is not guaranteed to be the shortest, because many characters are not satisfied when the other characters have been oversupply, how to remove this part of the superfluous? From the beginning to sweep the second long string, if the current character does not exist in the target string, you can pass directly, if it exists in the target string, and oversupply, then this character can be removed from our string, the equivalent of our string shortened, but remember the demand + +, because the supply is reduced. Knowing a character that exists both in the target string and that his demand is exactly equal to 0, we have to stop, because all this time is rigid demand, can no longer be reduced supply.

The code below, and there is no optimization, but the idea is written out.

Class Solution {public:string Minwindow (string S, String T) {int ct1[270], ct2[270];        int mlen1 = S.length ();        int mlen2 = T.length ();        memset (ct1, 0, sizeof (CT1));        memset (ct2, 0, sizeof (CT2));        int hole = mlen2, minSize = int_max, start = 0, Minstart;            for (int i=0;i<mlen2;i++) {ct1[t[i]]++;        ct2[t[i]]++;                } for (int i=0;i<mlen1;i++) {if (ct2[s[i]]>0) {ct1[s[i]]--;            if (ct1[s[i]]>=0) hole--;                        } if (hole = = 0) {while (START&LT;MLEN1) {if (ct2[s[start]]>0) {                        if (ct1[s[start]]<0) ct1[s[start]]++;                    else break;                } start++;                    } if (minsize>i-start+1) {minSize = i-start+1; Minstart = Start;        }}} if (minSize = = Int_max) return "";    Return S.substr (Minstart, minSize); }};


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.