[Interesting DP Problem]Good Strings

I am not able to understand the approach given in the editorial. If someone can explain the approach it will be nice.