-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathBrackets_v2.py
35 lines (29 loc) · 996 Bytes
/
Brackets_v2.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
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
# write your code in Python 3.6
my_stack = []
# note: use 'insert(index, item)' and 'pop(index)'
for char in S:
if char == '{' or char == '[' or char == '(':
my_stack.insert( len(my_stack), char)
# note: check if the stack is empty or not (be careful)
if len(my_stack) == 0:
return 0
elif char == ')':
pop = my_stack.pop( len(my_stack)-1 )
if pop != '(':
return 0
elif char == ']':
pop = my_stack.pop( len(my_stack)-1 )
if pop != '[':
return 0
elif char == '}':
pop = my_stack.pop( len(my_stack)-1 )
if pop != '{':
return 0
# note: check if the stack is empty or not (be careful)
if len(my_stack)!=0:
return 0
else:
return 1