forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy path20_Valid_Parentheses.py
More file actions
27 lines (24 loc) · 832 Bytes
/
20_Valid_Parentheses.py
File metadata and controls
27 lines (24 loc) · 832 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
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
parenthesesStack = []
parenthesesDictionary = {')': '(', '}': '{', ']': '['}
for char in s:
if char in parenthesesDictionary.values():
parenthesesStack.append(char)
continue
if char in parenthesesDictionary.keys():
if len(parenthesesStack) != 0:
openBracket = parenthesesStack.pop()
else:
return False
if openBracket is not None and parenthesesDictionary[char] == openBracket:
continue
else:
return False
if len(parenthesesStack) != 0:
return False
return True