Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Naive but cached solution in Clear category for Remove Brackets by Phil15
from functools import lru_cache
@lru_cache # 13s without it, 0.6s with it...
def remove_brackets(brackets: str) -> str:
if is_balanced(brackets):
return brackets
# Delete from the beginning, return longest one.
return max((remove_brackets(brackets[:i] + brackets[i + 1:])
for i in range(len(brackets))), key=len)
def is_balanced(s: str) -> bool:
old = None
while s != old:
old, s = s, s.replace('()', '').replace('[]', '').replace('{}', '')
return not s
April 6, 2020
Comments: