Top 150 InterviewEasy

Valid Parentheses

Detailed guide and Python implementation for the 'Valid Parentheses' problem.

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?
Consider using Stack-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.