How to solve this question. I tried using the state space machine approach buy not able to write the logic of transformation from one state to another. this is similar to one of the leetcode questions.