Python Basics簡単

Balanced Parenthesis Problem

Detailed guide and Python implementation for the 'Balanced Parenthesis Problem' problem.

問題提起

簡単

Write a function is_balanced(s) that takes a string s containing only the characters '(', ')', '{', '}', '[' and ']', and returns True if the parentheses are balanced and properly nested, False otherwise. An empty string is considered balanced.

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

Example 1
Input
s = '({[]})'
Output
True
Explanation

Each opening bracket has a matching closing bracket in the correct order: ( matches ), { matches }, [ matches ].

Example 2
Input
s = '({[})'
Output
False
Explanation

The [ is closed by } which is a mismatch.

Example 3
Input
s = ''
Output
True
Explanation

An empty string is considered balanced.

Need a Hint?
Consider using Arrays-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 リソース

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