Algorithms Playbook
← All problems
EasyStacks & queues →Similar to: Valid Parentheses

Do the brackets match?

Problem

Given a string containing only (, ), [, ], { and }, return whether it is valid: every opening bracket is closed by the same type of bracket, and brackets close in the correct order.

Examples

Input: "{[()]}"

Output: true

Input: "([)]"

Output: false

`]` arrives while the last open bracket is `(`.

Input: "(("

Output: false

Hints

    Try it first. Every hint reveals a bit more.

    Solution