Problem 204493 · easy · Level 02 Linear Data Structures

Bracket Matcher for the Editor

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.

A code editor colours a line red when its brackets do not pair up. The line s is made only of the characters (, ), [, ], { and }. Return True when the line passes: each closing bracket must close the most recent bracket that is still open, and that bracket must be of the same kind; a closing bracket with nothing open fails, and so does any bracket still open at the end. Otherwise return False.

Examples

Input:  s = "{()}[]"
Output: True
Input:  s = "[{]}"
Output: False
Explanation: the "]" arrives while "{" is the most recent open bracket.
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…