1 class Solution:
 2     def minWindow(self, s: str, t: str) -> str:
 3         n = len(s)
 4         if n==0:
 5             return ""
 6         if len(t)==0:
 7             return s
 8         #if s == t:
 9         #    return s
10         dic = {}
11         dic2= {}
12         for i in range(len(t)):
13             dic.update({t[i]:0})
14             if t[i] in dic2.keys():
15                 dic2.update({t[i]:dic2[t[i]]+1})
16             else:
17                 dic2.update({t[i]:1})
18         #dicount = len(t)
19         i = 0
20         j = 0
21         minstr = ""
22         minwidth = n
23         while i <=j and j<n :
24             #j++
25             if s[j] in t:
26                 dic.update({s[j]:dic[s[j]]+1})
27                 valid = True
28                 for k in dic.keys():
29                     if dic[k]<dic2[k]:
30                         valid = False
31                         break
32                 if valid:
33                     #valid
34                     curlen = j-i+1
35                     if curlen<=minwidth:
36                         minwidth = curlen
37                         minstr = s[i:j+1]
38                     #i++
39                     while i<=j:
40                         if s[i] not in dic.keys():
41                             i += 1
42                         else:
43                             if dic[s[i]]>dic2[s[i]]:
44                                 dic.update({s[i]:dic[s[i]]-1})
45                                 i += 1
46                             else:
47                                 curlen = j-i+1
48                                 if curlen <= minwidth:
49                                     minwidth = curlen
50                                     minstr = s[i:j+1]
51                                 break
52                 #else:
53                     #invalid
54 
55             j += 1
56 
57         return minstr

 

posted on 2019-03-06 21:45  Sempron2800+  阅读(227)  评论(0编辑  收藏  举报