FastPrepWord Search with Eight-Direction Adjacency

Word Search with Eight-Direction Adjacency

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a square character grid board and a nonempty string word, return true if the word can be formed by a path through the grid.

From each cell, the path may move to any of its eight horizontal, vertical, or diagonal neighbors. A path may change direction between characters, but it cannot use the same cell more than once.

Function

wordExists8Directions(board: char[][], word: String) → boolean

Examples

Example 1

board = [["A","B"],["C","D"]]word = "AD"return = true

The path moves diagonally from the upper-left A to the lower-right D.

Example 2

board = [["A","B"],["C","D"]]word = "ABA"return = false

The only A would need to be reused after visiting B, which is forbidden.

Constraints

  • 1 <= board.length <= 6
  • board[i].length = board.length
  • 1 <= word.length <= board.length * board.length
  • The grid and word contain uppercase and lowercase English letters, matched case-sensitively.

More Microsoft problems

See Microsoft hiring insights
public boolean wordExists8Directions(char[][] board, String word) {
  // Write your code here.
}
board[["A","B"],["C","D"]]
word"AD"
expectedtrue
Checking account…