-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrackets.py
More file actions
33 lines (31 loc) · 902 Bytes
/
Brackets.py
File metadata and controls
33 lines (31 loc) · 902 Bytes
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
def checkIfCont(char, char_list):
for c in char_list:
if c==char:
return True
return False
def solution(S):
if len(S) == 0 or not S:
return 1
if len(S)%2 ==1:
return 0
left_brackets = ['{','[','(']
right_brackets = ['}',']',')']
if checkIfCont(S[0], right_brackets):
return 0
if checkIfCont(S[-1], left_brackets):
return 0
tmp_bra_list = []
for i in range(len(S)):
if checkIfCont(S[i],left_brackets):
tmp_bra_list.append(S[i])
elif checkIfCont(S[i], right_brackets):
bra_idx = right_brackets.index(S[i])
if len(tmp_bra_list) == 0:
return 0
if tmp_bra_list[-1] == left_brackets[bra_idx]:
tmp_bra_list.pop()
else:
return 0
if len(tmp_bra_list) > 0:
return 0
return 1