I’m looking for an efficient way to chance a string such that all sequences of more than 2 equal characters are cut off after the first 2.
Some input->output examples are:
hellooooooooo -> helloo
woooohhooooo -> woohhoo
I’m currently looping over the characters, but it’s a bit slow. Does anyone have another solution (regexp or something else)
EDIT: current code:
word_new = ""
for i in range(0,len(word)-2):
if not word[i] == word[i+1] == word[i+2]:
word_new = word_new+word[i]
for i in range(len(word)-2,len(word)):
word_new = word_new + word[i]
Edit: after applying helpful comments
(original response here)
Try something like this: