Leetcode first _ Minimum Window Substring

Source: Internet
Author: User

Good question, String, linear time.

I think the person who got this question for the first time should not know how to do it, or I am too weak .. First, find out what is required for this question. Find a string from a long string. The character in this string completely contains the character in another given target string, and the length of this string must be minimal. There is also a very important simplification. The stem indicates that there is only one shortest string. This restriction actually implies the overall idea of this question, as long as you find a long string, then you can scale it down to the point where it cannot be scaled down.

Starting from the requirements of the question, we can find that this question has no requirements on the Character Sequence in the string, so we can naturally think of using the hash table to store the number of each character in the target string, then, find a string in the source string that contains more than or equal to the number of characters in the target string. It is difficult to implement this function.

The number of characters in the target string can be used as its requirement. Each time a character is found in a long string and this character exists in the target string, the requirement should be reduced by 1, when the requirement is equal to 0, it indicates that the characters in the long string can meet the requirements of the character in the target string. If the demand for a character in the target string changes to a negative value, this indicates that the supply of this character in the long string is excessive under the current length. For ease of description, I define that when a long string needs this character in the target string for the timing, to meet the rigid requirement, whether or not there is a supply surplus. Next, we have another question: how can we know that the target string is fully satisfied? Of course, you can change the scanning requirements one by one to non-positive, but there is also a simpler method, that is, to regard the length of the target string as the total demand. When meeting rigid requirements, the total demand is reduced by 1. When the total demand changes to 0, the target string is satisfied.

The process described above finds a string in a long string that can fully satisfy the target string, but it is not guaranteed to be the shortest, because many characters are oversupplied when other characters are not satisfied, how can we remove these extra characters? Scanning long strings from the start point to the end. If the current character does not exist in the target string, you can directly pass the string. If the character exists in the target string and the supply is surplus, this character can be removed from our strings, which is equivalent to shortening our strings, but remember to add more than one character because the supply is reduced. Knowing that a character is stored in the target string and its demand is equal to or equal to 0, we have to stop, because all the requirements at this time are rigid, supply cannot be reduced.

The Code is as follows, and there is no optimization, but the idea is to write it 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<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.