[LC] 76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

Time: O(N)

class Solution:
    def minWindow(self, s: str, t: str) -> str:
        res = ‘‘
        my_dict = {}
        for char in t:
            freq = my_dict.get(char, 0)
            my_dict[char] = freq + 1
        count, start, end, min_len = len(my_dict), 0, 0, sys.maxsize
        res_end, res_start = 0, 0
        while end != len(s):
            char = s[end]
            if char in my_dict:
                my_dict[char] -= 1
                if my_dict[char] == 0:
                    count -= 1
            end += 1

            while count == 0:
                start_char = s[start]
                if start_char in my_dict:
                    my_dict[start_char] += 1
                    if my_dict[start_char] > 0:
                        count += 1            

                if min_len > end - start:
                    min_len = end - start
                    res_end = end
                    res_start = start
                start += 1

        return s[res_start: res_end]
            

原文地址:https://www.cnblogs.com/xuanlu/p/11660721.html

时间: 2024-08-28 04:08:18

[LC] 76. Minimum Window Substring的相关文章

Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)

目录 问题描述 例子 解决方案 ** Leetcode 76 ** 问题描述 Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). 例子 Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC&qu

76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will 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 i

[leedcode 76] Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will 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 i

leetCode 76.Minimum Window Substring(最小窗口子串) 解题思路和方法

Given a string S and a string T, find the minimum window in S which will 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 windo

76. Minimum Window Substring (Map)

Given a string S and a string T, find the minimum window in S which will 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 i

leetcode[76] Minimum Window Substring

给定两个串,S和T,在S中找到包含T的最短子串,如果不能包含,返回空字符. Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC&

leetcode 76 Minimum Window Substring ----- java

Given a string S and a string T, find the minimum window in S which will 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 i

LeetCode in Python 76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"Note: If there is no such window in S th

Minimum Window Substring leetcode

Minimum Window Substring Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: