You are viewing a single comment's thread. Return to all comments →
That's the one I'm most proud of so far, so here we go (it's in JS):
const pairs = { '{': '}', '(': ')', '[': ']', }; function isOpenBracket(symbol) { return Object.keys(pairs).includes(symbol); } function isBalanced(s) { const openBrackets = []; const brackets = s.split(''); for (let bracket of brackets) { if (isOpenBracket(bracket)) { openBrackets.push(bracket); } else { const lastOpenBracket = openBrackets[openBrackets.length - 1]; if (!lastOpenBracket) { return 'NO'; } if (pairs[lastOpenBracket] !== bracket) { return 'NO'; } openBrackets.pop(); } } return openBrackets.length === 0 ? 'YES' : 'NO'; }
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 →
That's the one I'm most proud of so far, so here we go (it's in JS):