Skip to contentKEEP THE CRAFTTHE SANCTUARY

Word Search in a Character Grid

Decide whether a word can be traced through neighbouring grid cells without reusing a cell.

The problem

Implement Solution.exist(board, word). board is a grid of letters. Return True if you can spell word by starting at any cell and moving one step at a time up, down, left or right, using each cell at most once in the word. Diagonal moves are not allowed. Otherwise return False. For example, in the grid [["A", "B"], ["C", "D"]] you can spell "ABD" but not "ABA" or "AD". Constraints: 1 to 6 rows and columns, a word of 1 to 15 letters, and only lowercase or uppercase English letters (case matters).

Starter code

class Solution:
    def exist(self, board, word):
        pass

Public examples

  1. board = [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word = "ABCCED"givesTrue
  2. board = [["a"]], word = "a"givesTrue
  3. board = [["A"], ["B"]], word = "AA"givesFalse

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.