-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumWindowSubstring.py
More file actions
36 lines (33 loc) · 1.08 KB
/
minimumWindowSubstring.py
File metadata and controls
36 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/minimum-window-substring/
# Author: Miao Zhang
# Date: 2021-01-14
# t for dicts
# traverse, if s[i] in dicts, dicts[i] -= 1, if dicts[i] >= 0: count += 1
# if count == len(t) collect minimum string
class Solution:
def minWindow(self, s: str, t: str) -> str:
from collections import defaultdict
dicts = defaultdict(int)
for c in t:
dicts[c] += 1
res = ''
l = 0
count = 0
minlen = len(s) + 1
for i in range(len(s)):
if s[i] in dicts:
dicts[s[i]] -= 1
if dicts[s[i]] >= 0:
count += 1
while count == len(t):
if minlen > i - l + 1:
minlen = i - l + 1
res = s[l: i + 1]
if s[l] in dicts:
dicts[s[l]] += 1
if dicts[s[l]] > 0:
count -= 1
l += 1
return res