ExamAdda Logo

Valid Parentheses

Easy

Given a string s containing only the characters:

( ) { } [ ]

 

determine if the input string is valid.

A string is valid if:

  1. Every opening bracket is closed by the same type of closing bracket.
  2. Opening brackets are closed in the correct order.
  3. Every closing bracket has a corresponding opening bracket.

Example 1

Input

()

Output

true

Explanation

The opening parenthesis ( is correctly closed by ).

Example 2

Input

()[]{}

Output

true

Explanation

All opening brackets have their corresponding closing brackets, and they are closed in the correct order.

Constraints

1 <= s.length <= 10^4
s consists of parentheses only:
'('
')'
'{'
'}'
'['
']'

Hints:

Hint 1

Use a stack to store opening brackets as you traverse the string.

Hint 2

Whenever you encounter a closing bracket, check whether the top of the stack contains its corresponding opening bracket.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 9, 2026

Last updated

Aug 10, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output