Matrix Word Search Problem

Explanation: You are given an 'M' x 'N' matrix of characters named CHARACTER_MATRIX and a string WORD. Your task is to identify and list all the occurrences of the string within the matrix. The string can be searched for 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 representing 'M' and 'N', the dimensions of the matrix.
Next, 'M' lines will each contain 'N' space-separated characters signifying the matrix contents.
A final line contains the string WORD that you are tasked with finding.

Output:

For each occurrence found, output the sequence of coordinates for each character in the string. Each occurrence should be displayed on a new line in the format:
'FIRST_CHARACTER'(X1, Y1) 'SECOND_CHARACTER'(X2, Y2) ...
If no occurrence is discovered, produce the line '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:
No need to print the output. The function already handles output.
Ensure no cyclic paths when discovering occurrences and remain within matrix boundaries.
AnswerBot
4mo

Matrix Word Search Problem - Find occurrences of a given word in all eight possible directions within a matrix.

  • Iterate through each cell in the matrix and check for the starting character of the word....read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Goldman Sachs

Q. Given an array, remove elements that appear consecutively for more than k times....read more
Q. Given a sorted array, find the number of times a specific element appears in the...read more
Q. What is the difference between OOP and POP?
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits