Indeed, linear time is possible for each length of the substring, since you only need consecutive identical substrings. Just keep a counter with identical characters and refresh the line when you find the substring. Since you want to remove substrings of all possible lengths, the overall complexity is quadratic.
The following C code should work:
char str[20]="abcababceccced"; int len = strlen(str); int i, j, counter; for(i = 1; i <= len / 2; ++i) { for(j = i, counter = 0; j < len; ++j) { if (str[j] == str[j - i]) counter++; else counter = 0; if (counter == i) { counter = 0; memmove(str + j - i, str + j, (len - j) * sizeof(char)); j -= i; len -= i; } } str[j] = 0; printf("%s\n", str); }
This should be printed sequentially:
abcababceced abcabced abced
source share