Back to Practice Dashboard
Top 150 InterviewEasy

Valid Parentheses

Learn how to solve the 'Valid Parentheses' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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

An input string is valid if:

1. Open brackets must be closed by the same type of brackets.

2. Open brackets must be closed in the correct order.

3. Every close bracket has a corresponding open bracket of the same type.

Write a function isValid(s: str) -> bool.

Constraints
  • 1 <= len(s) <= 10^4
  • s consists of parentheses only: '()[]{}'

Examples

Example 1
Input
s = "()"
Output
True
Explanation

A single pair of matching parentheses is valid.

Example 2
Input
s = "()[]{}"
Output
True
Explanation

Three pairs of matching brackets, each closed in order.

Example 3
Input
s = "(]"
Output
False
Explanation

Opening '(' is closed by ']' which is the wrong type.

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor