Valid Parentheses
Given a string s containing only the characters:
( ) { } [ ]
determine if the input string is valid.
A string is valid if:
- Every opening bracket is closed by the same type of closing bracket.
- Opening brackets are closed in the correct order.
- 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
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
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.
Expected Output