-
Notifications
You must be signed in to change notification settings - Fork 1
/
valid_parentheses.py
43 lines (32 loc) · 1.35 KB
/
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
34
35
36
37
38
39
40
41
42
43
class Solution:
def isValid(self, s: str) -> bool:
# main idea:
# A Last-In-First-Out (LIFO) data structure
# such as 'stack' is the perfect fit
if len(s)==0:
return True
valid_char = ['(', ')', '{', '}', '[', ']']
left_half = ['(', '[', '{']
right_half = [')', ']', '}']
# use a map/dictionary, which is more maintainable
another_half = {')': '(', '}':'{', ']':'['}
my_stack = []
for char in s:
if char not in valid_char:
return False
# we see an opening parenthesis, we push it onto the stack
if char in left_half:
my_stack.append(char)
# we encounter a closing parenthesis, we pop the last inserted opening parenthesis
if char in right_half:
if len(my_stack) == 0:
return False
# be careful: check if the pair is a valid match
elif another_half[char] != my_stack[len(my_stack)-1]:
return False
else:
my_stack.pop()
if len(my_stack) !=0:
return False
else:
return True