Question: Write a function that groups a string into cluster of brackets. Each cluster should be balanced. In a balanced cluster, every opening bracket must exist

  1. Write a function that groups a string into cluster of brackets. Each cluster should be balanced. In a balanced cluster, every opening bracket must exist with its matching closing parenthesis in the same cluster. The function returns all clusters in a list. If it contains an unbalanced parenthesis then returns an empty list. There is no limit of number of characters.

Supporting brackets are parenthesis, square bracket, and curly bracket. You can use built-in functions/libraries (standard libraries / no external installation)

def Balance(a_str):

// your code goes here

Example:

print(Balance("((()))"))

print(Balance("{(())}{}"))

print(Balance("{(()){}"))

#the function should return

["((()))"]

["{(())}","{}"]

[]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!