LeetCode Minimum Window Substring

Source: Internet
Author: User

LeetCode Minimum Window Substring
LeetCode-solving-Minimum Window Substring

Original question

Given two strings S and T, it is required to find the shortest substring of S containing all characters in T in O (n) time.

Note:

If no sub-string meets the requirements, "" is returned. If multiple sub-strings meet the requirements, only one of them is possible.

Example:

Input: s = "ADOBECODEBANC", t = "ABC"

Output: "BANC"

Solutions

Determine the current sub-string through the front and back pointers. Move the sub-string until the sub-string contains all the characters in T and try to move the front pointer back, and refresh the shortest length and the corresponding starting position. If the pre-pointer does not contain any characters in T, move the pre-pointer. Move the front and back pointers alternately until the complete string S is traversed.

AC Source Code
from collections import defaultdictclass Solution(object):    def minWindow(self, s, t):        """        :type s: str        :type t: str        :rtype: str        """        MAX_INT = 2147483647        start = end = 0        char_need = defaultdict(int)    # the count of char needed by current window, negative means current window has it but not needs it        count_need = len(t)             # count of chars not in current window but in t        min_length = MAX_INT        min_start = 0        for i in t:            # current window needs all char in t            char_need[i] += 1                   while end < len(s):            if char_need[s[end]] > 0:                count_need -= 1            # current window contains s[end] now, so does not need it any more            char_need[s[end]] -= 1                  end += 1            while count_need == 0:                if min_length > end - start:                    min_length = end - start                    min_start = start                # current window does not contain s[start] any more                char_need[s[start]] += 1                    # when some count in char_need is positive, it means there is char in t but not current window                if char_need[s[start]] > 0:                     count_need += 1                start += 1        return "" if min_length == MAX_INT else s[min_start:min_start + min_length]if __name__ == "__main__":    assert Solution().minWindow("ADOBECODEBANC", "ABC") == "BANC"

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.