Not Able To Get The Approach/Intuition for this problem

This problem looks similar to matrix chain multiplication but I am unable to figure out the unique states. Please help me!

To solve this problem you need 2 arrays. Suppose the max number of elements possible in the array is N.
So one array is possible[N][N][3] (this 3 is for β€˜a’,β€˜b’ and β€˜c’), so the state dp[i][j][k], where i<=j is used to store if its possible to reduce segment (i->j) to this character β€˜k’ where k can be β€˜a’,β€˜b’ or β€˜c’.

second array is dp[3][N], where 3 is again taken to represent every letter. And dp[i][j] represents the min length of string we can get if we reduce 'j’th character to β€˜i’( i can β€˜a’,β€˜b’ or β€˜c’),
so the answer would be min(dp[0][N-1], dp[1][N-1], dp[2][N-1]).

If you want I can share the implementation.

I was able to solve the problem. Thanks a lot for the help. I am marking this doubt as β€œresolved”. Thanks again! :slight_smile:

I also thought on mcm but i got stuck in this question. Now even after reading the above solution I cannot figure out what to do plz can you explain or share the code.