Top 150 Interview簡単

Valid Parentheses

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

問題提起

簡単

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.

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

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

解決する準備はできましたか?

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

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。