Given a string, your task is to collapse it into a zigzag-like structure as described below.
Folding into a Zigzag
We'll take the string
"Mississippi" as an example:
First, output the longest prefix consisting of unique characters only:
When you reach the first duplicate character C, ignore it, and output the longest prefix consisting of unique characters of the remaining string (
issippi) vertically, below the first occurrence of C:
Mis i s
Repeat the process, alternating between horizontal and vertical continuation. But now be careful (at step 1) to continue outputting horizontally from latest occurrence of the duplicate character, which is not necessarily the last one, as in this case:
Mis i sip ----- Mis i sip i
- The string will only contain printable ASCII characters, but won't contain any kind of whitespace.
- You can compete in any programming language and can take input and provide output through any standard method and in any reasonable format1, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest submission (in bytes) for every language wins.
- 1 Input: String / List of characters / whatever else your language uses to represent strings. Output: Multiline string, list of strings representing lines or list of lists of characters/length-1 strings, but please include a pretty-print version of your code in your answer, if possible.
- Regarding additional spaces, the output may contain:
- Leading / trailing newlines
- Trailing spaces on each line / at the end
- A consistent number of leading spaces on each line
- You must begin to output horizontally, you may not start vertically.
"Perfect" "Mississippi" "Oddities" "Trivialities" "Cthulhu" "PPCG" "pOpOpOpOpOpOp" "ABCCCE" "ABCCCECCEEEEC" "abcdcebffg" "abca" "AAAAAAAA"
Perf c t
Mis i sip i
Od ies t
Triv a l ies t
P C G
pO OpO pOp p
ABC C E
ABC CCE E EC
abcd e b fg
A AA A