-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.py
More file actions
27 lines (25 loc) · 814 Bytes
/
brackets.py
File metadata and controls
27 lines (25 loc) · 814 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
def countOpenBracket(bracketList, midindex):
count = 0
for i in range(midindex):
if bracketList[i] == "(":
count = count + 1
return count
def countCloseBracket(bracketList, midindex):
count = 0
for i in range(midindex, len(bracketList)):
if bracketList[i] == ")":
count = count + 1
return count
def solution(S):
bracketList = list(S)
bracketListSize = len(bracketList)
midindex = bracketListSize/2
while True:
openBracket = countOpenBracket(bracketList, midindex)
closeBracket = countCloseBracket(bracketList, midindex)
if openBracket > closeBracket:
midindex = midindex - 1
elif openBracket < closeBracket:
midindex = midindex + 1
else:
return midindex