-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20 Valid Parentheses.py
33 lines (30 loc) · 1.17 KB
/
20 Valid Parentheses.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
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
#create a stack to keep track of the open brackets
stack = []
#create a dictionary to keep track of the matching brackets
brackets = {')':'(', '}':'{', ']':'['}
#iterate through the string
for char in s:
#if the character is an open bracket, add it to the stack
if char in brackets.values():
stack.append(char)
#if the character is a close bracket, check if it matches the last open bracket
elif char in brackets.keys():
#if the stack is empty, return False
if not stack:
return False
#if the last open bracket does not match the close bracket, return False
if stack[-1] != brackets[char]:
return False
#if the last open bracket matches the close bracket, pop it from the stack
stack.pop()
return not stack
test = Solution()
print(test.isValid("()")) #True
print(test.isValid("()[]{}")) #True
print(test.isValid("(]")) #False