Hack the interview(Minimum String Co-Efficient(HARD))

my code=>Programming Problems and Competitions :: HackerRank

i got only 19 can someone tell me what will be approach for 40
Thanks in advance…