Having spend some time on this site I have come to enjoy things being as short as possible. That may be the reason why I'm recently kind of offended by strings containing the same characters more than once. Your job is to write a function or program which condenses a given string according to the following rules:
Start with a 0-condensation, that is look for the first (leftmost) pair of the same characters with 0 other characters between them. If such a pair is found, remove one of the two characters and restart the algorithm by performing another 0-condensation. If no such pair is found, proceed with the next step. Examples:
Then perform a 1-condensation, that is look for the first pair of same characters with 1 other character between them. If such a pair is found remove one of them and all characters between them and restart with a 0-condensation. If no such pair is found, proceed with the next step. Examples:
Continue with a 2-condensation and so on up to a n-condensation with n being the length of the original string, each time restarting after a condensation removed some letters. Examples:
The resulting string is called condensed and contains each character at most once.
A lower case string of printable ascii-characters.
The condensed string according to the rules above.
examples -> es
programming -> praming
puzzles -> puzles
codegolf -> colf
andromeda -> a
abcbaccbabcb -> acb
if(x==1):x++ -> if(x+
fnabnfun -> fun
abcdefae -> abcde
Detailed examples to clarify how the algorithm works:
fnabnfun -C0-> fnabnfun -C1-> fnabnfun -C2-> fnfun -C0-> fnfun -C1-> fun -C0-> fun
-C1-> fun -C2-> ... -C8-> fun
abcbaccbabcb -C0-> abcbacbabcb -C0-> abcbacbabcb -C1-> abacbabcb -C0-> abacbabcb
-C1-> acbabcb -C0-> acbabcb -C1-> acbcb -C0-> acbcb -C1-> acb -C0-> acb
-C1-> ... -C12-> acb
Your approach doesn't have to implement the algorithm from above as long as your solution and the algorithm return the same output for all allowed inputs. This is a code-golf challenge.
Thanks to @Linus for helpful sandbox comments!