Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-002

Balanced Brackets

Problem

Check whether a string of bracket characters is balanced.

The string may only contain the characters (), [], and {}. A string is balanced when every opening bracket is closed by the matching type in the correct order.

Return True if balanced, otherwise False. An empty string is balanced.

Hint

Push opening brackets; on a closing bracket the top of the stack must be its match.

Test cases

case 1
in: ["()[]{}"]
expected: true
case 2
in: ["(]"]
expected: false
case 3
in: ["([{}])"]
expected: true
case 4
in: [""]
expected: true
case 5
in: ["("]
expected: false
Starter codePython
def is_balanced(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor