Sometimes it happens that while typing a sentence, I am distracted and I end up typing the same couple of words twice couple of words twice in succession.
To make sure make sure other people are not bothered by this, your task is to write a program that resolves this problem!
Given an input string (if it matters for your language, you may assume ASCII-only input that does not contain linefeeds.)
str, that contains somewhere in its middle a substring that occurs twice in immediate succession, return the string with one instance of this substring removed.
In the case of multiple possibilities, return the shortest answer possible (that is, pick the longest consecutive repeating substring and remove that one).
In the case of multiple, equally-long consecutive repeating substrings, remove the first (that is, the first one encountered when reading through the string from front to back) one.
You may assume that the input is correct (i.e. always contains a consecutive repeating substring), which might help to golf it down.
hello hello world-> Output:
foo. (So: Yes, the string might only consist of the repeating part twice).
aaa, as the longest repeating consecutive substring is here
Slartibartfast-> This is not a valid input, as it does not contain a consecutive repeating substring, so you do not need to handle this case.
the few the bar-> This is another invalid input, since the repeating part should immediately follow the original part. In this case,
theare separated by something else in-between, so this input is invalid.
abcbc. The two possible longest consecutive repeating substrings are
abis encountered earlier in the string, this one is the correct answer.
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo. Output:
Buffalo buffalo buffalo buffalo Buffalo buffalo. (The performed replacement should be case-sensitive).
Sometimes it happens that while typing a sentence, I am distracted and I end up typing the same couple of words twice couple of words twice in succession.-> Output:
Sometimes it happens that while typing a sentence, I am distracted and I end up typing the same couple of words twice in succession.. Only the longest consecutive repeating substring is removed.
Your code should be as short as possible, since this is code-golf, so the shortest answer in bytes wins. Good luck!