Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
backtracking & memoization 🐘 solution in Clear category for Remove Brackets by vincent.tscherter
// do wie have valid brackets in w?
var valid = (w, _ = '') => w == '' || (w != (_ = w.replace(/\(\)|\[\]|\{\}/g,'')) && valid(_))
// caching results for better performance
var memo = {}
// do the thing caching subresults in for better performance
var removeBrackets = (w) => memo[w] = (memo[w] || valid(w))
? memo[w] || w
: [...w].reduce((a,_,i) =>
(_=removeBrackets(w.substr(0, i) + w.substr(+i+1))).length > a.length ? _ : a,
'')
June 8, 2020
Comments: