You are viewing a single comment's thread. Return to all comments →
Python
def isBalanced(s): stack = [] for i in s: if i == '(' or i == '[' or i =='{': stack.append(i) elif i == ')' and len(stack) == 0: return 'NO' elif i == ']' and len(stack) == 0: return 'NO' elif i == '}' and len(stack) == 0: return 'NO' elif i == ')' and stack[-1] == '(': stack.pop() elif i == ']' and stack[-1] == '[': stack.pop() elif i == '}' and stack[-1] == '{': stack.pop() else: return 'NO' if len(stack) > 0 : return 'NO' else: 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 →
Python