Given a string $t$, we want to make the string after cyclic left shift and cyclic right shift equal. When removing some characters from $t$ to satisfy this condition, what is the minimum number of characters to remove?
Let $n$ be the length of string $t$, and consider separately for even and odd cases.
When $n$ is odd
Take $t = t_1 t_2 t_3 t_4 t_5$ as an example. We want to make cyclic left shift $t_2 t_3 t_4 t_5 t_1$ and cyclic right shift $t_5 t_1 t_2 t_3 t_4$ equal. That is, all characters need to be equal.
When $n$ is even
Take $t = t_1 t_2 t_3 t_4$ as an example. We want to make cyclic left shift $t_2 t_3 t_4 t_1$ and cyclic right shift $t_4 t_1 t_2 t_3$ equal. That is, all even-positioned characters must be the same, and all odd-positioned characters must also be the same.
Solution
Perform an exhaustive search on the first two characters of the string after removing some characters. If the first two characters are the same, it’s good to have some characters $a \dots a$ follow the first two characters $aa$. If different, it’s good to have $ababab \dots ab$ follow the first two characters $ab$.