This is an alternate version of this earlier challenge with a twist that adds a significant bit of difficulty.
Like last time, you are going to be given a string containing some alphabetic characters along with [
and ]
. Your task is to split into sections that are enclosed in a "group" created by [...]
and those that are not. e.g.
"absbn[mesl]meslo[eyyis]me"
->
"absbn"
"mesl"
"meslo"
"eyyis"
"me"
However unlike last time, this time a "group" must be non-empty. This means it must contain at least one character. So when there are []
next to each other they don't form a group. For example:
"absm[]mm[m]"
->
"absm[]mm"
"m"
""
The first pair of brackets is empty so it can't form a group. The second one does so it counts as a group and we split it. Notice that the non-group portions can be empty, it's just the groups that cannot.
Additionally when there is a conflict where two possible groups could be made: like co[[x]t
or m[[e]]it
, we will always choose the smallest valid group. In the case of a tie we choose the group that starts the furthest to the left.
Any [
s left without a match are just regular characters and appear in the output.
So in co[[x]t
we could do co
[x
t
, but we could also do co[
x
t
. Since the group here is either [x
or x
we choose the smaller one and the result is co[
x
t
. In the second case me[[e]]it
there are 4 ways to make groups here, but unambiguously me[
e
]it
results in the smallest group.
If we have me[[]]yt
then the smallest group would be []
, but that's not a group, so our options are me [ ]yt
and me[ ] yt
so we choose me [ ]yt
because that group starts further to the left.
Your task will be to take a non-empty string of characters a
through z
plus two brackets of your choice ([]
, {}
, ()
and <>
) and to split it as described above, providing a list of strings as output.
In cases where a group is on the boundary of the string, e.g. aaa[b]
or [ems]ee
you may choose to include or omit an empty string ""
on that boundary. The test cases always include them.
This is code-golf. Answers will be scored in bytes with the goal being to minimize the size of your source code.
Test cases
go[cat]me -> "go" "cat" "me"
absbn[mesl]meslo[eyyis]me -> "absbn" "mesl" "meslo" "eyyis" "me"
absm[]mm[m] -> "absm[]mm" "m" ""
co[[x]t -> "co[" "x" "t"
me[[e]]t -> "me[" "e" "]t"
me[[]]yt -> "me" "[" "]yt"
mon[g]]u -> "mon" "g" "]u"
msy]mesl -> "msy]mesl"
eoa[m -> "eoa[m"
a[b[ -> "a[b["
mesi]mmp[mo -> "mesi]mmp[mo"
meu[ems[mela[] -> "meu[ems" "mela[" ""
w[[[] -> "w[" "[" ""
[foo]foobar[bar] -> "" "foo" "foobar" "bar" ""
ab[][]cd -> "ab" "][" "cd"
me[[[][]]yt -> "me[","[","","]","yt"
mem[[[[]]] -> "mem[[" "]" "]]"
mem[ooo[]]t -> "mem[ooo" "]" "t"