Skip to content
BASA DSA

Word Search

MediumBacktracking

Return whether word can be traced in a letter board through orthogonally adjacent cells. A cell may not be reused in the same path.

Example 1

0,0A
0,1B
1,0D
1,1C
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
board = [["A","B"],["D","C"]]
word = "ABCD"
Output
true

Explanation

A->B->C->D moves right, down and left without revisiting a cell.

Example 2

0,0A
0,1B
1,0D
1,1C
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
board = [["A","B"],["D","C"]]
word = "ABA"
Output
false

Explanation

A->B->A would return from (0,1) to the already-used starting cell (0,0), which is forbidden.

Example 3

0,0X
Input grid. Cell values are shown at their original row and column; coordinates start at zero.
Input
board = [["X"]]
word = "X"
Output
true

Explanation

The only cell matches the one-letter word.

Constraints

  • 1 <= rows, columns <= 6
  • 1 <= len(word) <= 15
  • Board and word contain English letters

Function contractexistArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…