ntree.ai
ntree.aiWord Search

Word Search

mediumsolvedattempted

You are given an m x n grid of letters board and a string word.

Return true if word can be spelled by a path through the grid. The path moves between cells that are horizontally or vertically next to each other, and it may not use the same cell twice.

Example 1:

Input:  board = [["A", "B", "C", "E"],
         ["S", "F", "C", "S"],
         ["A", "D", "E", "E"]],
        word = "ABCCED"
Output: true

Example 2:

Input:  board = [["A", "B", "C", "E"],
         ["S", "F", "C", "S"],
         ["A", "D", "E", "E"]],
        word = "ABCB"
Output: false
Explanation: spelling it would need the "B" cell twice.

Constraints:

  • 1 <= m, n <= 6
  • 1 <= length of word <= 15
  • board and word are made of uppercase and lowercase English letters
def exist(board: list[list[str]], word: str) -> bool:
pass
 

Press Visualise to step through your code.