Medium · Matrices

Word search on board

Given an m × n grid of letters board and a string word, return true if word can be spelled along a path of horizontally or vertically adjacent cells that uses each cell at most once, otherwise false.

Examples

Example 1

{
  "board": [
    ["A", "B", "C", "E"],
    ["S", "F", "C", "S"],
    ["A", "D", "E", "E"]
  ],
  "word": "ABCCED"
}

Output: "ABCCED" found

Example 2

{
  "board": [
    ["A", "B", "C", "E"],
    ["S", "F", "C", "S"],
    ["A", "D", "E", "E"]
  ],
  "word": "ABCB"
}

Output: "ABCB" not found

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Matrices problems