Haplology
Haplology is a linguistic term:
the omission of one occurrence of a sound or syllable that is repeated within a word, for example probly for probably.
For this challenge, it means specifically, replacing any sequences of two or more letters that are repeated two or more times with just one copy.
For example:
- haplology -> haplogy
- boobook -> book
- couscous -> cous
- ngorongoro -> ngoro
- hehehe -> he
- whakakakakaka -> whaka
- lerderderg -> lerderg
If there are multiple, non-overlapping repeating sequences, they all get replaced:
- cancangogo -> cango
- yadayadablahblah -> yadablah
Only a single round of replacements is performed, taking the longest possible sequences first, then working from left to right:
- mississippi -> missippi
- mymememymemebooboo -> mymemeboo
- aaaabaaaab -> aaaab
This means the returned result can contain repeating sequences:
- babambambadingding -> babambading
It cans also mean the end result is longer that it would be if replacement happened strictly left to right:
- bababababababadoobababababababadoo -> bababababababadoo (not badoobadoo)
Any spaces or hyphens that occur between elements of a repeating section must be stripped out, and otherwise retained:
- lang lang -> lang
- cha-cha-cha -> cha
- hi-di-hi-di-hi-di-ho -> hi-di-ho
- tut-tutting -> tutting
- lady gaga -> lady ga
- banana-cocoa agar agar -> bana-coa agar
- who put the bop in the bop shoo bop shoo bop who put the dip in the dip da dip da dip -> who put the bop in the bop shoo bop who put the dip in the dip da dip
- hare krishna hare krishna krishna krishna hare hare hare rama hare rama rama rama hare hare -> hare krishna krishna hare hare rama rama hare
Challenge
Write a function/program/etc which applies haplology, as defined above, to a single input string.
Inputs and outputs
Each input will match this regex: ^[a-z]([a-z -]?[a-z])*$
Your output is a lowercase string containing the haplologised input. No extraneous characters.
Standard rules for taking input and output.
Scoring
Code golf. Standard rules and exclusions apply.
Updates
Additional test cases and clarification
The algorithm (ignoring punctuation) is:
- find the longest chunk of letters that is repeated at least twice, giving precedence to the left-most chunk
- replace those repeated chunks with one chunk
- repeat, until none left
- never allow any any letter to be processed as part of different chunks
- mamapapatatat -> mapatat (there is no duplicated sequence longer than 2 characters, so work left to right: mama, papa, tata)
- babababa -> baba ([baba] x2)
- ratratatat -> ratat ([rat] x2, [at] x2)
Questions
Why does babambambadingding give babambading instead of bambading (from [baba][mbamba][dingding])?
In order of priority, the chunks are [ding], [bam]. The chunk [mba] is the same length as [bam], but [bam] occurs to its left.
Why doesn't the "hare krishna" test case greedily take the third consecutive hare?
Because the longer [hare rama] takes precedence over the shorter [hare]
lan glang -> lan glang
andlan g lang -> lan g lang
, butlan g lan g -> lan g
? \$\endgroup\$