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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Matrices problems