The challenge is simple, find the matching parentheses for every one of the parentheses in a given string input
E.g.
()()() -> [1, 0, 3, 2, 5, 4]
Always start with the ending parentheses index before the starting parentheses, which is why ()
becomes [1,0]
, not [0,1]
, unless the parentheses is stacked e.g. (())
, where the position of the parentheses must match the index so (())
is [3, 2, 1, 0]
Test Cases:
() -> [1, 0]
()(()) -> [1, 0, 5, 4, 3, 2]
()()() -> [1, 0, 3, 2, 5, 4]
((())) -> [5, 4, 3, 2, 1, 0]
(()(())) -> [7, 2, 1, 6, 5, 4, 3, 0]
You may output the indexes in the form of a list, or in the form of a string representation if so, but the output indexes must be distinguishable from each other e.g. 1 0
You are allowed to use 0-index or 1-indexed lists/arrays.
This is code-golf so shortest bytes wins!
(()(()))
. \$\endgroup\$