Find All Occurrences in Matrix
You are provided with a matrix of characters, CHARACTER_MATRIX
of size M x N
, and a string WORD
. Your goal is to locate and display all occurrences of the string within the matrix. The search for the string can be conducted in all eight possible directions: North, South, East, West, North-East, North-West, South-East, and South-West.
Input:
The first line of input contains two space-separated integers 'M' and 'N' indicating the dimensions of the matrix 'CHARACTER_MATRIX'.
Next M lines contain N space-separated characters denoting the elements of the matrix.
The following line contains the string WORD.
Output:
For each occurrence, print the coordinates of each character in the format:
‘FIRST_CHARACTER’(X1, Y1) ‘SECOND_CHARACTER’(X2, Y2) ...
If no occurrences are found, output 'No match found'.
Example:
Input:
[ 'D', 'E', 'X', 'X', 'X' ]
[ 'X', 'O', 'E', 'X', 'E' ]
[ 'D', 'D', 'C', 'O', 'D' ]
[ 'E', 'X', 'E', 'D', 'X' ]
[ 'C', 'X', 'X', 'E', 'X' ]
WORD = "CODE"
Output:
'C'(2, 2) 'O'(1, 1) 'D'(0, 0) 'E'(0, 1)
'C'(2, 2) 'O'(1, 1) 'D'(2, 0) 'E'(3, 0)
'C'(2, 2) 'O'(1, 1) 'D'(2, 1) 'E'(1, 2)
'C'(2, 2) 'O'(1, 1) 'D'(2, 1) 'E'(3, 0)
'C'(2, 2) 'O'(1, 1) 'D'(2, 1) 'E'(3, 2)
'C'(2, 2) 'O'(2, 3) 'D'(2, 4) 'E'(1, 4)
'C'(2, 2) 'O'(2, 3) 'D'(3, 3) 'E'(3, 2)
'C'(2, 2) 'O'(2, 3) 'D'(3, 3) 'E'(4, 3)
Constraints:
- 1 <= M, N <= 100
- 1 <= WORD LENGTH <= 5
- Time Limit: 1 sec
Note:
Your implementation function does not need to print anything. It's already handled.

AnswerBot
4mo
Find all occurrences of a given string in a matrix in all eight possible directions.
Iterate through each cell in the matrix and check for the starting character of the word.
For each starting character...read more
Help your peers!
Add answer anonymously...
Paytm Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
A Software Developer Intern was asked Q. Binary Tree Diameter Problem Statement You are given a Binary Tree, and you need...read more
A Software Developer Intern was asked Q. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
A Software Developer Intern was asked Q2. Binary Tree Diameter Problem Statement You are given a Binary Tree, and you need...read more
A Software Developer Intern was asked Q3. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

