You are given a matrix of 'N' rows and 'M' columns consisting of lowercase characters. You are provided with a string ‘STR,’ and your task is to find the length of the longest consecutive character path for each character in the string ‘STR’. All characters in the path are consecutive, i.e., every character in the path is next to the previous character in alphabetical order. It is allowed to move in all 8 adjacent directions from a cell.
Note:Suppose you are at position (x, y) then you can move to : [ (x + 1 , y ), ( x - 1 , y ), ( x , y + 1), (x , y - 1), (x + 1, y + 1), (x + 1, y - 1), (x - 1, y + 1), (x - 1, y - 1) ].
For Example:
If ‘N’ = 2, ‘M’ = 2, 'STR’ = "a" and the input matrix is
[ [ ab ]
[ ed ] ]
We need to find the maximum length of the path from starting character ’a’. So the maximum length is 2, which is [a, b]. Hence, the answer is [2].
The first line contains a single integer 'T' representing the number of test cases.
The first line of each test case contains two integers, ‘N’ and ‘M’, denoting the number of rows and columns of the character array ‘matrix’.
The next ‘N’ lines of each test case contain a string consisting of ‘M’ characters denoting the elements of the array 'matrix'.
The last line of each test case contains the string ‘STR‘, which contains starting characters(without space).
Output Format :
For each test case, print the space-separated array of integers where each integer is the length of the longest path from the character at that index in the string ‘STR’.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= M <= 300
1 <= N <= 300
‘a’ <= matrix[i][j], STR[i] <= ‘z’
1 <= |STR[i]| <= 26
Time Limit: 1 sec
2
2 3
aef
pbc
ba
4 5
abcde
udjfi
mtgna
kunyq
afm
2 3
7 2 1
In test case 1:
There is a matrix of characters with ‘N’ = 2 and ‘M’ = 3.
The length of the path with ‘b’ as starting character is 2, which is [b, c].
The length of the path with ‘a’ as the starting character is 3, which is [a, b, c]
Hence, the answer is [2, 3].
In test case 2:
There is a matrix of characters with ‘N’ = 4 and ‘M’ = 5.
The length of the path with ‘a’ as the starting character is 7, which is [a, b, c, d, e, f].
The length of the path with ‘f’ as the starting character is 2, which is [f, g].
The length of the path with ‘m’ as the starting character is 1, which is [m]
Hence, the answer is [7, 2, 1].
2
2 4
aefg
pbcd
ec
3 2
ab
cd
ef
a
3 2
6
Try to think of a recursive solution.
In this approach, we are going to implement a recursive function. We will traverse the array matrix for each character of STR as a starting character. We will check if matrix[i][j] is equal to STR[index] then, we will create a function call to find the longest path.
Algorithm:
O((N*M)^2 * (length of STR)) where N and M are the numbers of rows and columns in the array matrix, and STR is the given string.
We will traverse through STR that takes O(length of STR) time. For each iteration, we will traverse through the character array that takes O(N*M) time, and also, for each element of the array, we call the recursive function that takes O(N*M) time. Hence, the overall time complexity is O((N*M)^2 * (length of STR)).
O(N*M) where N and M are the numbers of rows and columns in the array matrix.
The space complexity O(N*M) is required for the recursion stack in the worst case. Hence, the overall space needed is O(N*M).