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.
- Balanced Brackets
- Discussions
Balanced Brackets
Balanced Brackets
Sort by
recency
|
244 Discussions
|
Please Login in order to post a comment
Python:
Python 3
def isBalanced(s): stk = [] n = len(s) for i in range(n): if s[i] == '{' or s[i] == '[' or s[i] == '(': stk.append(s[i]) else: if (stk and ((s[i] == '}' and stk[-1] == '{') or (s[i] == ']' and stk[-1] == '[') or (s[i] == ')' and stk[-1] == '('))): stk.pop() else: return "NO" # unmatched closing bracket if stk: return "NO" else: return "YES"
My answer with Typescript.
Idea is remove couple of bracket when it can, then check remaining [s], if it empty mean it fully valid brackets