Balanced Brackets

  • + 0 comments

    python with a map

    brackets = {
        "}": "{",
        ")": "(",
        "]": "["
    }
    
    def isBalanced(s):
        # Write your code here
        stack = []
        for x in s:
            if x in brackets.values():
                stack.append(x)
            elif stack and brackets[x] == stack[-1]:
                stack.pop()
            else:
                return "NO"
        if not stack:
            return "YES"
        return "NO"