DSA Trainer

Valid Parentheses

StackEasy

Step 1 of 8 · Understand

1. Understand the problem

Problem statement

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

Input

A string `s` containing only the characters `(`, `)`, `{`, `}`, `[`, `]`.

Output

A boolean. `true` if every opening bracket is closed by the correct type in the right order, `false` otherwise.

Examples

input → s = "()"
output → true
input → s = "()[]{}"
output → true
Three separate valid pairs, none nested.
input → s = "(]"
output → false
The open paren is closed by a bracket, wrong type.
input → s = "([)]"
output → false
The brackets are interleaved incorrectly: [ is closed before ( is closed.

Write your solution above and hit Run tests. Results and tips appear here.