-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.py
More file actions
26 lines (25 loc) · 681 Bytes
/
Copy path20.py
File metadata and controls
26 lines (25 loc) · 681 Bytes
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
# Valid Parentheses
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
top = -1
stack = []
for i in s:
if (i == ')' and top !=-1 and stack[top]=='('):
stack.pop()
top-=1
elif i == '}' and top !=-1 and stack[top]=='{':
stack.pop()
top-=1
elif i == ']' and top !=-1 and stack[top]=='[':
stack.pop()
top-=1
elif i in "({[":
stack.append(i)
top+=1
else:
return False
return top == -1