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
- 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
Get step-by-step solutions from verified subject matter experts
