-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_parentheses
More file actions
37 lines (31 loc) · 1.25 KB
/
valid_parentheses
File metadata and controls
37 lines (31 loc) · 1.25 KB
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
class Solution(object):
# a function that returns a Boolean expression, so its either True or False
# input is s
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
# if the length of the list is not even, return False as it is definitely wrong
if (len(s)) % 2 != 0:
return False
# create a dictionary to store the key value pairs
dict = {'(': ')', '{': '}', '[': ']'}
# create an empty list
stack = []
for i in s:
# use .keys() function to check if character is present in any keys of the dictionary, ie. the character must be an open bracket
if i in dict.keys():
stack.append(i)
# if the character is a close bracket
else:
# assign variable a to the left most value in the stack ie. an open bracket
if stack == []:
return False
a = stack.pop()
# if the value pair of key a and the current close bracket character are not the same, return False
if i != dict[a]:
return False
if stack == []:
return True
return False