FastPrepEight-Direction Word Search

Eight-Direction Word Search

Motive logoMotive● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a rectangular grid of characters encoded as equal-length strings and a word, return whether the word can be formed from sequentially adjacent cells. Each step may move horizontally, vertically, or diagonally, and one cell cannot be reused within a path.

Function

wordExistsEightDirections(board: String[], word: String) → boolean

Examples

Example 1

board = ["ABCE","SFCS","ADEE"]word = "ABCCED"return = true

Example 2

board = ["AB","CD"]word = "AD"return = true

The diagonal move is allowed.

Constraints

  • 1 <= board.length, board[i].length <= 50.
  • 1 <= word.length <= 50.
  • Board cells and the word contain English letters.

More Motive problems

See Motive hiring insights
public boolean wordExistsEightDirections(String[] board, String word) {
  // write your code here
}
board["ABCE","SFCS","ADEE"]
word"ABCCED"
expectedtrue
Checking account…