Given a properly-parenthesized string as input, output a list of all nonempty substrings within matching parentheses (or outside of all parentheses), with nested parentheses removed. Each substring should be the sequence of characters in exactly the same matching parentheses. Substrings should be listed in order of depth, and substrings of the same depth should be listed in the order they occur in the string. Assume the input is always correctly parenthesized.
You may assume that the input contains only lower-case ASCII letters and parentheses.
Your answer should be a function that, when given a string, returns a list of strings.
Examples:
'a(b)c(d)e' -> ['ace', 'b', 'd']
'a(b(c)d)e' -> ['ae', 'bd', 'c']
'a((((b))))' -> ['a', 'b']
'a()b' -> ['ab']
'' -> []
'a' -> ['a']
'(((a(b)c(d)e)f)g)h' -> ['h', 'g', 'f', 'ace', 'b', 'd']
'ab(c(((d)ef()g)h()(i)j)kl)()' -> ['ab', 'ckl', 'hj', 'efg', 'i', 'd']
Fewest bytes wins.
'i'
and'd'
in the correct order in the last test case? \$\endgroup\$i
is less deeply nested thand
. \$\endgroup\$