Sanket and strings

I’m using this approach to solve this question, but output is not correct, So can you please check my code and let me know where i’m wrong.
Thanks.