Back to Practice Dashboard
Python BasicsEasy

Balanced Parenthesis Problem

Learn how to solve the 'Balanced Parenthesis Problem' problem. This detailed resource details brute force and optimized approaches.

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?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
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