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.
Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter