Python BasicsEasy

Balanced Parenthesis Problem

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

Problem Statement

Easy

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.

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

Examples

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

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.