Problem 6
Valid Parentheses
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.