All problems

Problem 6

Valid Parentheses

Easy · 10 pts · String, Stack

Problem

You are given a string made only of the bracket characters (, ), {, }, [ and ]. Decide whether the brackets are valid. A string is valid when every opening bracket is closed by a bracket of the same type, brackets close in the correct order (the most recently opened bracket must be closed first), and every closing bracket has a matching opening bracket before it. An empty string is valid. A stack is the natural tool: push each opening bracket, and for each closing bracket check that the top of the stack is its matching opener.

Input

A single line containing the bracket string.

Output

Print true if the brackets are valid; otherwise print false.

Constraints

0 <= string length <= 100000

Example input

()[]{}

Example output

true

Explanation

In ()[]{} each pair opens and is closed immediately by the same type: ( with ), [ with ] and { with }. Nothing is left open at the end, so the answer is true.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Maximum Subarray Next: Factorial of a Number