Word Search Problem Statement

Given a two-dimensional grid of size N x M consisting of upper case characters and a string 'WORD', determine how many times the 'WORD' appears in the grid.

The 'WORD' can be formed starting from any grid cell, moving in any of the eight possible directions: horizontally left, horizontally right, vertically up, vertically down, and the four diagonals.

Input:

N = 5
M = 5
Grid:
"AXYXB"
"NIAXX"
"AXNJN"
"XNIXA"
"JAXIX"
WORD = "NINJA"

Output:

3

Example:

Explanation:

The word 'NINJA' is found 3 times in the grid.

Constraints:

  • 1 <= N <= 10^3
  • 1 <= M <= 10^3
  • 1 <= N*M <= 10^6
  • 2 <= WORD.size <= 10^3

Time Limit: 1 second

Note:

You do not need to handle printing, focus on implementing the logic to compute the result.

Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Analyst 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