Problem 204493 · easy · Phase 02 Linear Data Structures

Valid Parentheses

stacks · strings

A stack is a last-in, first-out container: you only ever add to the top and remove from the top. In Python a plain list works as a stack via append (push) and pop (pop). Stacks shine whenever the most recently opened thing must be closed first, exactly like brackets.

Given a string s containing only the characters (, ), [, ], { and }, return True if the brackets are valid: every opening bracket is closed by the same type of bracket, and brackets are closed in the correct order. Otherwise return False.

Examples

Input:  s = "()[]{}"
Output: True
Input:  s = "([)]"
Output: False
Explanation: the "[" is closed by ")" before "]".
Input:  s = "{[]}"
Output: True

Constraints

  • 0 <= len(s) <= 10**4
  • The empty string is valid

Goals

  • Use a list as a stack with append and pop
  • Match nested opening and closing symbols in last-in-first-out order
Starting Python…