leetcode76 Minimum Window Substring

Posted yawenw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode76 Minimum Window Substring相关的知识,希望对你有一定的参考价值。

 1 """
 2 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).
 3 Example:
 4 Input: S = "ADOBECODEBANC", T = "ABC"
 5 Output: "BANC"
 6 """
 7 """
 8 题很难,debug一遍才看明白。debug过程如下:
 9 初始化:count{A:1, B:1, C:1}                    cnt = 0    滑动窗口[]
10        1.   {A:0, B:1, C:1}                    cnt = 1    [A]
11        2.   {A:0, B:1, C:1, D:-1}              cnt = 1    [AD]
12        3.   {A:0, B:1, C:1, D:-1, O:-1}        cnt = 1    [ADO]
13        4.   {A:0, B:0, C:1, D:-1, O:-1}        cnt = 2    [ADOB]
14        5.   {A:0, B:0, C:1, D:-1, O:-1, E:-1}  cnt = 2    [ADOBE]
15        6.   {A:0, B:0, C:0, D:-1, O:-1, E:-1}  cnt = 3    [ADOBEC]
16        ----------------cnt==len(t)时将结果保存-----------------------
17        ----------------开始移动窗口左端------------------------------
18        7.   {A:1, B:0, C:0, D:-1, O:-1, E:-1}  cnt = 2    [DOBEC]
19        ----------------cnt!=len(t),开始移动窗口右端-------------------
20        8.   {A:1, B:0, C:0, D:-1, O:-2, E:-1}  cnt = 2    [DOBECO]
21        9.   {A:1, B:0, C:0, D:-2, O:-2, E:-1}  cnt = 2    [DOBECOD]
22        10.  {A:1, B:0, C:0, D:-2, O:-2, E:-2}  cnt = 2    [DOBECODE]
23        11.  {A:1, B:-1, C:0, D:-2, O:-2, E:-2}  cnt = 2   [DOBECODEB]
24        12.  {A:0, B:-1, C:0, D:-2, O:-2, E:-2}  cnt = 3    [DOBECODEBA]
25        --------------如果小于之前的长度,则保存,移动左端窗口------------
26        13.  {A:0, B:-1, C:0, D:-1, O:-2, E:-2}  cnt = 3    [OBECODEBA]
27        14.  {A:0, B:-1, C:0, D:-1, O:-1, E:-2}  cnt = 3    [BECODEBA]
28        15.  {A:0, B:0, C:0, D:-1, O:-1, E:-2}  cnt = 3    [ECODEBA]
29        16.  {A:0, B:0, C:0, D:-1, O:-1, E:-1}  cnt = 3    [CODEBA]
30        17.  {A:0, B:0, C:1, D:-1, O:-1, E:-1}  cnt = 2    [ODEBA]
31        --------------cnt!=len(t),开始移动窗口右端-------------------
32        18.  {A:0, B:0, C:1, D:-1, O:-1, E:-1, N:-1}  cnt = 2    [ODEBAN]
33        19.  {A:0, B:0, C:0, D:-1, O:-1, E:-1, N:-1}  cnt = 3    [ODEBANC]
34        -----------果小于之前的长度,则保存,移动左端窗口------------------
35        20.  {A:0, B:0, C:0, D:-1, O:0, E:-1, N:-1}  cnt = 3    [DEBANC]
36        21.  {A:0, B:0, C:0, D:0, O:0, E:-1, N:-1}  cnt = 3    [EBANC]
37        22.  {A:0, B:0, C:0, D:0, O:0, E:0, N:-1}  cnt = 3    [BANC]
38        23.  {A:0, B:1, C:0, D:0, O:0, E:0, N:-1}  cnt = 2    [ANC]
39 """
40 class Solution:
41     def minWindow(self, s, t):
42         import collections
43         res = ""
44         left, cnt, minLen = 0, 0, float(inf)
45         count = collections.Counter(t)
46         for i, c in enumerate(s):
47             count[c] -= 1
48             if count[c] >= 0:
49                 cnt += 1
50             while cnt == len(t):
51                 if minLen > i - left + 1:
52                     minLen = i - left + 1
53                     res = s[left : i + 1]
54                 count[s[left]] += 1
55                 if count[s[left]] > 0:
56                     cnt -= 1
57                 left += 1
58         return res

 

以上是关于leetcode76 Minimum Window Substring的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode76.Minimum Window Substring

leetcode 76. Minimum Window Substring

Leetcode 76: Minimum Window Substring

LeetCode in Python 76. Minimum Window Substring

leetcode 76-Minimum Window Substring(hard)

leetcode76 Minimum Window Substring