We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It can be optimized with regex, but here is a solution that can help solving many balancing issues. the idea is to strip all the valid brackets, till we are left with no valid ones.
Solution is in Typescript:
functionisBalanced(s:string):string{// Write your code hereletreplacedCount=Infinity;while(replacedCount>0){conststart=s.lengths=s.replace('()','').replace('{}','').replace('[]','');constend=s.length;replacedCount=start-end;}returns.length===0?'YES':'NO';}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
It can be optimized with regex, but here is a solution that can help solving many balancing issues. the idea is to strip all the valid brackets, till we are left with no valid ones. Solution is in Typescript: