Word Search

Given a 2D board and a target word, check if the target word is in the board.

Edges are horizontal and vertical

Example

Board
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

(board, "ABFD") => true
(board, "SEE") => true
(board, "ABFA") => false

Approach

We want to find a valid path. BFS is designed for layer search. Path search is DFS's job.

DFS Code

Time Complexity

The worst case is an exhaustive search so exponential time. O(4^len(word)).

Word Search II (LC.212arrow-up-right)

Use a trie to accelerate the prefix search

Last updated