Task:
Input will consist of some text, with nested []
separating some of it into nested groups:
abc[d[fgijk]nopq]rst[u[v[[w]]]xy[[[z]]]]
Your task is to remove all brackets, and everything wrapped in an odd number of brackets. For example, a[b]c
would remove the []
, and the b
inside of it. a[[b]]c
, however, would only remove the brackets (as b
is wrapped in an even number of brackets). If the string were instead a[b[c]d]f
, the result would be acf
, as the b
and d
are wrapped in an odd number of brackets, but c
is not.
The correct output for the input given at the top would be:
abcfgijkrstvwz
I/O:
You can take input as a string, or any reasonable representation of one (like an array of characters). You can output in a similar fashion, with trailing whitespacing being allowed. You can assume all non-bracket characters are lowercase letters, and you can choose whether the brackets are ()
, []
, or {}
.
You can assume the brackets are properly nested (e.g., [
or ][
won't be given as inputs), and that there is at least one letter in the input.
Test cases:
abc abc
a[b]c ac
[ab]c c
ab[c] ab
[]abc abc
a[]bc abc
abc[] abc
a[[b]]c abc
a[[[b]]]c ac
a[b][c]d ad
a[b][]c ac
[a[b]c] b
a[b[c]d]f acf
[ab]c[df] c
a[a]a[[a]] aaa
abc[d[fgijk]nopq]rst[u[v[[w]]]xy[[[z]]]] abcfgijkrstvwz
zyx[w]v[ut[s]r[[qp][kj]i]gf][d][][][c][b[][a]] zyxvsia
Other:
This is code-golf, so shortest answer (in bytes) per language wins!