def parentheses(brackets):
openBrackets = []
closeBrackets = []
for char in range(len(brackets)):
if brackets[char] == '(':
openBrackets.append(char)
elif brackets[char] == ')':
closeBrackets.append(char)
pairs = []
for o in reversed(range(len(openBrackets))):
for c in range(len(closeBrackets)):
if openBrackets[o] < closeBrackets[c]:
pairs.append([])
pairs[-1].append(openBrackets[o])
pairs[-1].append(closeBrackets[c])
closeBrackets.remove(closeBrackets[c])
break
listBrackets = list(brackets)
filter = []
for i in range(len(pairs)):
if pairs[i][0]+1 == pairs[i][1]:
filter.append(pairs[i][0])
filter.append(pairs[i][0])
for x in range(len(pairs)):
if pairs[i][0]-1 in pairs[x] and pairs[i][1]+1 in pairs[x]:
filter.append(pairs[i][0]-1)
filter.append(pairs[i][1]+1)
for index in sorted(filter, reverse=True):
del listBrackets[index]
if not listBrackets:
return 'NULL'
return ''.join(listBrackets)
print(parentheses('((a((bc)(de)))f)'))
print(parentheses('(((zbcd)(((e)fg))))'))
print(parentheses('ab((c))'))
print(parentheses('(((3)))'))
print(parentheses('()'))
print(parentheses('((fgh()()()))'))
print(parentheses('()(abc())'))