You are viewing a single comment's thread. Return to all comments →
you need to validate this case '{{}' when the stack length is > 0 because there are more opening brackets than the closing ones
function isBalanced(s) { const stack = []; let i = 0; while (i <= s.length) { switch (s[i]) { case "(": case "[": case "{": stack.push(s[i]); break; case ")": if (stack.pop() !== "(") return "NO"; break; case "]": if (stack.pop() !== "[") return "NO"; break; case "}": if (stack.pop() !== "{") return "NO"; break; } i++; } if (stack.length > 0) return "NO"; return "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 →
you need to validate this case '{{}' when the stack length is > 0 because there are more opening brackets than the closing ones