Sherlock and the Valid String | HackerRank

anyone help me out in this question.