Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Remove Brackets by sawako.oono
def remove_brackets(line: str) -> str:
Open=[[],[],[]]
Closed=[]
Tent=[]
dic2={"(":0,"[":1,"{":2}
dic1={")":0,"]":1,"}":2}
for i in range(len(line)-1,-1,-1):
if line[i] in dic1.keys():
Open[dic1[line[i]]].append(i)
Tent.append(dic1[line[i]])
else:
if Open[dic2[line[i]]]!=[]:
if Tent[-1]==dic2[line[i]]:
Closed.extend([i,Open[dic2[line[i]]].pop()])
Tent.pop()
else:
while Tent[-1]!=dic2[line[i]]:
Open[Tent.pop()].pop()
Closed.extend([i,Open[dic2[line[i]]].pop()])
Tent.pop()
else:
pass
res="".join([line[x] for x in sorted(Closed)])
return res
if __name__ == '__main__':
print("Example:")
print(remove_brackets('(()()'))
# These "asserts" are used for self-checking and not for an auto-testing
assert remove_brackets('(()()') == '()()'
assert remove_brackets('[][[[') == '[]'
assert remove_brackets('[[(}]]') == '[[]]'
assert remove_brackets('[[{}()]]') == '[[{}()]]'
assert remove_brackets('[[[[[[') == ''
assert remove_brackets('[[[[}') == ''
assert remove_brackets('') == ''
assert remove_brackets('[(])') == '()'
print("Coding complete? Click 'Check' to earn cool rewards!")
June 21, 2021