-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20. Valid Parentheses
37 lines (36 loc) · 1.31 KB
/
20. Valid Parentheses
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
#link to problem https://leetcode.com/problems/valid-parentheses/description/
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
check if empty or odd (can't pass)
loop through string
have dict of key value pairs for closed brackets to open brackets
have list of closed brackets
have list to represent stack
check if bracket is open, if so add it to stack
else check if stack is non-empty and last element is a match
if so return true, else return false
once done if stack is empty return true
"""
if len(s) == 0 or len(s) % 2 != 0:
return False
bracket_dict = {')':'(', ']':'[','}':'{'}
closed_brackets = [')',']','}']
stack = []
for bracket in s:
#check if bracket is open
if bracket not in closed_brackets:
stack.append(bracket)
else:
try:
#if you can't access stack[-1], must be invalid
if bracket_dict[bracket] == stack[-1]:
stack.pop()
else:
return False
except:
return False
#stack must be empty to pass
return len(stack) == 0