You are viewing a single comment's thread. Return to all comments →
JS
function isBalanced(string) { const pairs = new Map([[")", "("], ["}", "{"], ["]", "["]]); const brackets = []; let balanced = true; for (let bracket of string) { if (pairs.has(bracket)) { if (brackets.pop() != pairs.get(bracket)) { balanced = false; break; } } else { brackets.push(bracket); } } return (balanced && brackets.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 →
JS