Given a list of characters in $\{a,b\}$, for example $abababababa$, what is the most efficient way to remove all 5th powers in a way that makes the string as short as possible?
(This example would reduce to a since the $(ab)^5$ cancels.) By 5th power I mean any contiguous* 5-fold repetition.
*Contiguous here means directly next to each other, so $aabaaa$ does not reduce to $[b]$ by canceling the $aaaaa$.
Note the order of reductions may give different results: $a^2(a^3 b^2)^5 \rightarrow a^2$, but also $\rightarrow (b^2 a^3)^4 b^2$. Also, the search may need to be done more than once, for example $a^2 b^5 a^3 \rightarrow> a^5 \rightarrow empty$. I would like to solve this optimization problem to make the resulting word as short as possible.
I haven't found a solution better than a rather naive sliding window approach that checks for repetitions of length 5, 10, 15, etc. but I suspect this may be improved with more advanced string processing algorithms I am unfamiliar with. Of course there are exponentially many words so it is inefficient to simply generate all words of length $l$ such that $5l$ is less than the length of the input, then run Aho-Corasick to check for each despite that being linear time.
Solutions or advice for where else to look welcome.