You are viewing a single comment's thread. Return to all comments →
JS solution:
function isBalanced(s) { if (s.length % 2 != 0) return "NO" const PAIRS = { "}": "{", "]": "[", ")": "(" } const brackets = [] for (let i = 0; i < s.length; i++) { if (!(s[i] in PAIRS)) { brackets.push(s[i]) } else { let counterBracket = brackets.pop() if (PAIRS[s[i]] !== counterBracket) brackets.push(null) } } return brackets.length > 0 ? "NO" : "YES" }
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 solution: