eb0c6551
2016-12-29 04:15:26 +08:00
这种技巧性很强的题目并不值得花很多时间自己想。以及熟练掌握技巧后,应该是可以很快地白板写出来的,我写的:
import collections
class Solution(object):
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
missing = len(t)
need = collections.defaultdict(lambda: 0)
best = ' '*(len(s)+1)
for c in t:
need[c] += 1
slow = 0
for fast in range(len(s)):
need[s[fast]] -= 1
missing -= need[s[fast]] >= 0
while missing == 0:
best = min(best, s[slow:fast+1], key=lambda x: len(x))
need[s[slow]] += 1
missing += need[s[slow]] > 0
slow += 1
return best if len(best) < len(s) + 1 else ''