Consecutive Characters
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].
Input Format :
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.
Constraints :
1 <= T <= 10
1 <= M <= 300
1 <= N <= 300
‘a’ <= matrix[i][j], STR[i] <= ‘z’
1 <= |STR[i]| <= 26
Time Limit: 1 sec
CodingNinjas
author
2y
Recursive Approach
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] i...read more
CodingNinjas
author
2y
Dynamic Programming
As we have discussed in the previous approach, we will create a recursive function to find the longest path. This approach will maintain a dynamic array dpMatrix that stores the max...read more
Anonymous
8mo
good
Add answer anonymously...
Top Info Edge Test Engineer interview questions & answers
Popular interview questions of Test Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app