← Back to challenges

Parentheses Clusters

PythonHardstringsdata_structures

Instructions

Write a function that groups a string into parentheses clusters. Each cluster should be balanced.

Examples

split("()()()") ➞ ["()", "()", "()"]

split("((()))") ➞ ["((()))"]

split("((()))(())()()(()())") ➞ ["((()))", "(())", "()", "()", "(()())"]

split("((())())(()(()()))") ➞ ["((())())", "(()(()()))"]

Notes

  • All input strings will only contain parentheses.
  • Balanced: Every opening parens ( must exist with its matching closing parens ) in the same cluster.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.