Consider the following alphabetically sorted list of words:
balderdash
ballet
balloonfish
balloonist
ballot
brooding
broom
All of the words start with b
, and the first 5 start with bal
. If we just look at the first 2 words:
balderdash
ballet
we could write instead:
balderdash
+let
where the ' '
is used where a word shares a prefix character with the previous word; except for the '+'
character which indicates the LAST character where the second word shares a prefix with the previous word.
This is a sort of 'trie' visualization: the parent is 'bal
', and has 2 descendants: 'derdash'
and 'let'
.
With a longer list, such as:
balderdash
ballet
brooding
we can additionally use the pipe character '|'
to make it clearer where the shared prefix ends, as follows:
balderdash
| +let
+rooding
and the equivalent tree would have a root of 'b'
having two children: the subtree having root 'al'
and and its two children 'derdash'
and 'let'
; and 'rooding'
.
If we apply this strategy to our original list,
balderdash
ballet
balloonfish
balloonist
ballot
brooding
broom
we get output that looks like:
balderdash
| +let
| +oonfish
| | +ist
| +t
+rooding
+m
If two consecutive words in the list have no shared prefix, no special characters are substituted; e.g. for the list:
broom
brood
crude
crumb
we want the output:
broom
+d
crude
+mb
Input
The words in the input will be made up of only alphanumerics (no spaces or punctuation); this may be in the form of a list of strings, a single string, or any other reasonable approach, as long as you specify your chosen format. No two consecutive words will be the same. The list will be alphabetically sorted.
Output
Your output can contain trailing whitespace per line or in total, but no leading whitespace. A list of strings or similar would also be acceptable.
This is code-golf; the shortest code in each language retains bragging rights. The usual prohibitions against loopholes apply.
Test Cases
Input:
apogee
apology
app
apple
applique
apply
apt
Output:
apogee
|+logy
+p
|+le
| +ique
| +y
+t
Input:
balderdash
ballet
balloonfish
balloonist
ballot
brooding
broom
donald
donatella
donna
dont
dumb
Output:
balderdash
| +let
| +oonfish
| | +ist
| +t
+rooding
+m
donald
| |+tella
| +na
| +t
+umb
ball
afterballoon
. What output should we expect? \$\endgroup\$+
under the firsto
, but I didn't write the challenge so I'm not certain. \$\endgroup\$