easy: balanced brackets

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.
using only a stack
stack = []

for _, v in enumerate(s):
    # return False if the char is a closing bracket and the list is empty
    if (v == ')' or v == ']' or v == '}') and not stack:
        return False
    # if open bracket, add to stack
    elif v == '(' or v == '[' or v == '{':
        stack.append(v)
    # if ) and the top of the stack is (, pop
    elif v == ')' and stack[-1] == '(':
        stack.pop()
    # if ] and the top of the stack is [, pop
    elif v == ']' and stack[-1] == '[':
        stack.pop()
    # if } and the top of the stack is {, pop
    elif v == '}' and stack[-1] == '{':
        stack.pop()
    # otherwise return false
    else:
        return False

# safeguard against s being all open brackets
return not stack
using a stack and map
stack = []
bracketMap = {')': '(', ']': '[', '}': '{'}

for _, v in enumerate(s):
    # return False if the char is a closing bracket and the list is empty
    if v in bracketMap and not stack:
        return False
    # if open bracket, add to stack
    elif v not in bracketMap:
        stack.append(v)
    # if ) and the top of the stack is (, pop
    elif stack[-1] == bracketMap[v]:
        stack.pop()
    # otherwise return false
    else:
        return False

# safeguard against s being all open brackets
return not stack

Subscribe to rohp

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe