Skip to content

Commit 9f67102

Browse files
committed
Week 6: Valid Parentheses
1 parent ded9dcc commit 9f67102

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

valid-parentheses/mandel-17.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class ArrayStack:
2+
def __init__(self, capacity):
3+
self.capacity = capacity
4+
self.array = [None] * self.capacity
5+
self.top = -1
6+
7+
def isEmpty(self):
8+
return self.top == -1
9+
10+
def isFull(self):
11+
return self.top == self.capacity - 1
12+
13+
def push(self, item):
14+
if not self.isFull():
15+
self.top += 1
16+
self.array[self.top] = item
17+
else:
18+
pass
19+
20+
def pop(self):
21+
if not self.isEmpty():
22+
self.top -= 1
23+
return self.array[self.top+1]
24+
else:
25+
pass
26+
27+
class Solution:
28+
def isValid(self, s: str) -> bool:
29+
stack = ArrayStack(100)
30+
for ch in s:
31+
if ch in ('(', '[', '{'):
32+
stack.push(ch)
33+
elif ch in (')', ']', '}'):
34+
if stack.isEmpty():
35+
return False
36+
else:
37+
left = stack.pop()
38+
if (ch == ')' and left != '(') or \
39+
(ch == ']' and left != '[') or \
40+
(ch == '}' and left != '{'):
41+
return False
42+
return stack.isEmpty()
43+
44+

0 commit comments

Comments
 (0)