Write a program that groups a string into parentheses cluster. Each cluster should be balanced.
Examples :
split("((())d)") ➞ ["((()))"]
split("(h(e(l)l)o)(w(o)r)l(d)(w)h(a(t)(s)u)p") ➞ ["((()))", "(())", "()", "()", "(()())"]
split("((())())(()(()()))") ➞ ["((())())", "(()(()()))"]
Input may contain letters other than parentheses but you are not sorting them. (The possible characters in the input are uppercase and lowercase letters, numbers, and ()
)
Given parentheses will always be complete (i.e : (
will be matched with )
)
Cluster is when outermost (
ends with )
. In short when your parentheses are all balanced at that moment so :
((( ---> this will be balanced when 3 ) are added
))) ---> now its balanced this is cluster 1
() ---> this is cluster 2
(((((((((((((((((((((((((((((( ---> this is start of cluster 3. will need equal amount of ) to balance
This is code-golf, shortest code wins
(Link to challenge on Code Wars : link for those that wish to check it)