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!
Task
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.
Examples
- Input:
hello hello world
-> Output:hello world
. - Input:
foofoo
-> Output:foo
. (So: Yes, the string might only consist of the repeating part twice). - Input:
aaaaa
-> Output:aaa
, as the longest repeating consecutive substring is hereaa
. - Input:
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. - Input:
the few the bar
-> This is another invalid input, since the repeating part should immediately follow the original part. In this case,the
andthe
are separated by something else in-between, so this input is invalid. - Input:
ababcbc
-> Output:abcbc
. The two possible longest consecutive repeating substrings areab
andbc
. Asab
is encountered earlier in the string, this one is the correct answer. - Input:
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
. Output:Buffalo buffalo buffalo buffalo Buffalo buffalo
. (The performed replacement should be case-sensitive). - Input:
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!
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.
as input, the output should beSometimes 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 found duplication is removed. \$\endgroup\$p
inhappens
\$\endgroup\$