Skip to content

Word Search

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Given a grid of letters board (a list of rows, each a list of single characters) and a word, return whether the word can be traced in the grid. You trace by starting on any cell and moving one step up, down, left or right for each next letter. A cell cannot be used twice in the same word.

board = [["A","B","C","E"],
         ["S","F","C","S"],
         ["A","D","E","E"]]
 
word = "ABCCED"  →  True
word = "SEE"     →  True
word = "ABCB"    →  False   (would need to reuse the B)

Constraints

  • 1 <= rows, cols <= 6
  • 1 <= word.length <= 15
  • board and word contain only upper and lower case English letters.