Skip to content

Commit 11fdabb

Browse files
committed
Adding Efficient Solution for Problem - 76 - Minimum Window Substring
1 parent ac4a075 commit 11fdabb

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 76
6+
## Problem Name: Minimum Window Substring
7+
##===================================
8+
#
9+
#Given a string S and a string T,
10+
#find the minimum window in S which will contain all the characters in T in complexity O(n).
11+
#
12+
#Example:
13+
#
14+
#Input: S = "ADOBECODEBANC", T = "ABC"
15+
#Output: "BANC"
16+
import collections
17+
class Solution:
18+
def minWindow(self, s, t):
19+
tmp = collections.Counter(t) #Initilaize tmp dictionary > key = letter and value = count
20+
left, right, i, j, minStr = 0, 0, 0, 0, len(t) #Initilaize left, right, i, j, and minStr
21+
while right < len(s): #Loop while right is less than the length of string
22+
if tmp[s[right]] > 0: #Condition-check: If string's right value is greater than zero
23+
minStr -= 1 #Update minStr by decreasing 1
24+
tmp[s[right]] -= 1 #Update dictionary value for particular key by decreasing 1
25+
right += 1 #Update right by increasing 1
26+
while minStr == 0: #Loop while minStr is zero
27+
if j == 0 or right - left < j - i: #Condition-check: If j is zero or right - left value is less than j - i value
28+
i, j = left, right #Update i, j by left, right
29+
tmp[s[left]] += 1 #Update dictionary value for particular key by increasing 1
30+
if tmp[s[left]] > 0: #If dictionary value for particular key is greater than 0
31+
minStr += 1 #Update minStr by increasing 1
32+
left += 1 #Update left by increasing 1
33+
return s[i:j] #Finally return s from i to j index

0 commit comments

Comments
 (0)