Recursion duplicate char formatting

Is there a better way of solving this question(compared my code)?

@sam15jain hey samyak here is my implemented code