Push opens, pop and match on closes. Stack must be empty at end.
function isValid(s): stack = [] pairs = {')': '(', ']': '[', '}': '{'} for char in s: if char in '([{': stack.push(char) else: if not stack or stack.pop() != pairs[char]: return false return len(stack) == 0
time, space.