

If some path has the letters “xyxxxyzz” then the value of this path will be 4 as the maximum frequency of any letter(‘x’) is 4.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains two integers separated by a single space ‘N’ and ‘M’ denoting the number of the nodes and the number of edges respectively.
The next line contains a string of length ‘N’ consisting of lowercase letters, denoting the character assigned to each node. The 'i’-th character of the string is assigned to the 'i’-th node.
The next ‘M’ lines of each test case contain two integers, ‘X’ and ‘Y’ separated by a single space, ‘X’ and ’Y’ denote the vertices connected by an edge directed from ‘X’ to ‘Y’.
For each test case, return the maximum value of any path.
You don’t need to print anything, It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^5
0 <= M <= 10^5
|S| = N
1 <= X,Y <= N
Where |S| denotes the length of the string.
Time limit: 1 sec
The idea is to explore all possible paths and then take the maximum value out of all the paths.
We can see that if the graph contains a cycle then we can keep traversing the cycle, again and again, hence the answer will be infinitely large, thus in case a cycle is present in the graph we should return -1.
For finding a cycle we will use the depth first search algorithm, to check whether any back edge exists in the graph or not. If we find any back edge then that means there exists a cycle in the graph.
The steps are as follows:
The idea is to use dynamic programming so that we don’t traverse any node more than once.
Let’s define a 2-D array ‘dp’, ‘dp[i][j]’ will denote the maximum number of letters ‘j’ that we can have when we reach the i’th node. (We will denote ‘a’ by 0, ‘b’ with 1, and so on). The value of ‘i’ will range from 1 to N and the value of ‘j’ will range from 0 to 25 since there are 26 distinct characters. In order to get the value ‘dp[i][j]’, we will need to visit all nodes which have an edge directed towards node ‘i’ before visiting the i’th node. Thus we follow the topological sorting order when visiting the nodes.
We can see that if the graph contains a cycle then we can keep traversing the cycle, again and again, hence the answer will be infinitely large, thus in case a cycle is present in the graph we should return -1.
The steps are as follows :