Skip to content

Commit 73bc351

Browse files
authored
Merge pull request vJechsmayr#190 from suparna13/master
0076_Minimum_Window_Substring
2 parents 938e52b + a438545 commit 73bc351

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from collections import defaultdict
2+
3+
class Solution:
4+
def minWindow(self, s: str, t: str) -> str:
5+
char_frequency = defaultdict(lambda: 0)
6+
for c in t:
7+
char_frequency[c] += 1
8+
chars_matched = 0
9+
10+
start = 0
11+
res = ""
12+
13+
for end in range(len(s)):
14+
right_char = s[end]
15+
if right_char in t:
16+
char_frequency[right_char] -= 1
17+
if char_frequency[right_char] == 0:
18+
chars_matched += 1
19+
20+
while start <= end and chars_matched == len(char_frequency):
21+
if res == "" or end-start+1 < len(res):
22+
res = s[start:end+1]
23+
24+
left_char = s[start]
25+
if left_char in t:
26+
if char_frequency[left_char] == 0:
27+
chars_matched -= 1
28+
char_frequency[left_char] += 1
29+
start += 1
30+
31+
return res

0 commit comments

Comments
 (0)