Why my code is giving MLE?

I was solving Simple Enough question, Why I’m getting MLE?

Plz help me find out the error in my code.

@siddharth_1_3277,
Because you are trying to make string of all the indexes, even the one which are useless, and thus the length of such string will be O(3^{log(n)}) , which is too much.