Sanket And Strings

I am not able to submit my solution . Also i think my solution is quite big. Can someone help with a different approach . My solution https://ide.codingblocks.com/s/138850. The code is correct and giving correct answer for any test case.