Skip to content

Commit b68839f

Browse files
author
wuduhren
committed
update
1 parent 53d11bc commit b68839f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def minWindow(self, s: str, t: str) -> str:
3+
if len(t)>len(s): return ""
4+
5+
ans = ""
6+
counter1 = collections.Counter(t)
7+
charSet = set(t)
8+
counter2 = collections.Counter() #sliding window in string s, index between l and r
9+
charSet2 = set(s)
10+
matchCount = 0 #count of char in the sliding window that counts are larger than the char count in t.
11+
12+
l = 0
13+
for r in range(len(s)):
14+
counter2[s[r]] += 1
15+
16+
if s[r] in charSet and counter1[s[r]]==counter2[s[r]]: matchCount += 1
17+
18+
while l<len(s) and (s[l] not in charSet or counter2[s[l]]>counter1[s[l]]):
19+
counter2[s[l]] -= 1
20+
l += 1
21+
22+
if matchCount==len(charSet) and (ans=="" or r-l+1<len(ans)):
23+
ans = s[l:r+1]
24+
25+
return ans
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
3+
ans = []
4+
q = collections.deque()
5+
l = r = 0
6+
7+
while r<len(nums):
8+
while q and nums[q[-1]]<nums[r]: q.pop()
9+
q.append(r)
10+
11+
while q and q[0]<l: q.popleft()
12+
13+
while r-l+1==k:
14+
ans.append(nums[q[0]])
15+
l += 1
16+
17+
r += 1
18+
return ans

problems/python3/valid-parentheses.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
mapping = {')': '(', ']': '[', '}':'{'}
4+
stack = []
5+
6+
for c in s:
7+
if c not in mapping:
8+
#open parentheses
9+
stack.append(c)
10+
else:
11+
#close parentheses
12+
if stack and stack[-1]==mapping[c]:
13+
stack.pop()
14+
else:
15+
return False
16+
17+
return not stack

0 commit comments

Comments
 (0)