-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4.py
42 lines (36 loc) · 1.05 KB
/
p4.py
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
37
38
39
40
41
42
class myStack:
def __init__(self, aList = None):
if aList is None:
aList = []
self.lst = aList
def push(self, x):
self.lst.append(x)
def pop(self):
return self.lst.pop()
def isEmpty(self):
return self.lst == []
class Solution:
def isValid(self, s):
stack = myStack()
for char in s:
if char == '(' or char == '[' or char == '{':
stack.push(char)
elif char == ')':
if stack.pop() != '(':
return False
elif char == ']':
if stack.pop() != '[':
return False
elif char == '}':
if stack.pop() != '{':
return False
return stack.isEmpty()
if __name__ == '__main__':
s = "()(){(())"
print(Solution().isValid(s))
s = ""
print(Solution().isValid(s))
s = "([{}])()"
print(Solution().isValid(s))
s = "(((([{}]))))()[]{}{[](){}]"
print(Solution().isValid(s))