Balanced Brackets

  • + 0 comments
    def isBalanced(s):
        # Write your code here
        stack = []
        left_brackets = '([{'
        bracket_map = {')': '(', ']': '[', '}': '{'}
        for c in s:
            if c in left_brackets:
                stack.append(c)
            else:
                if stack and stack[-1] == bracket_map[c]:
                    stack.pop()
                else:
                    return 'NO'
        return 'NO' if stack else 'YES'