Substring Problem in Graph

Ques : Problem - 919D - Codeforces
Code: Submission #114628248 - Codeforces

I fail test case number 5. Can someone help me figure out the mistake?
I basically applied dfs to the graph while checking for the cycle, If I find a cycle I simply return true and print -1 or else keep track of the number of occurrences of alphabets, and as soon as we reach the leaf node i.e. graph node with no children, I update the max (ans) value.

Kindly help!

I don’t think that this check is required while calculating the max path value.

if (adjList[src].empty()) {

then how do will check if I am the leaf node? Somehow it’s passing test case 5 and not test case 6. Something’s still wrong!

There is some problem in the max ans calculation, it will be better to try once by storing max values for all the nodes for each of the 26 characters, and then find max ans from that.