Skip to main content

Command Palette

Search for a command to run...

79. Word Search

Published
•2 min read•View as Markdown
B
Senior Software Engineer | Backend, Cloud, Data, Machine Learning

Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

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 = "SEE"
Output: true

Example 3:

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

Constraints:

  • m == board.length

  • n = board[i].length

  • 1 <= m, n <= 6

  • 1 <= word.length <= 15

  • board and word consists of only lowercase and uppercase English letters.

Solution:

class Solution:

    def exist(self, board: List[List[str]], word: str) -> bool:
        m = len(board)
        n = len(board[0])

        for i in range(m):
            for j in range(n):
                if board[i][j] == word[0]:
                    if self.dfs(board, word, 0, i, j, m, n):
                        return True

        return False

    def dfs(self, board: List[List[str]], word: str, index: int, i: int, j: int, m: int, n: int):
        if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[index]:
            return False
        if index == len(word) - 1:
            return True

        board[i][j] = '#'

        if (self.dfs(board, word, index + 1, i - 1, j, m, n)
            or self.dfs(board, word, index + 1, i + 1, j, m, n)
            or self.dfs(board, word, index + 1, i, j - 1, m, n)
            or self.dfs(board, word, index + 1, i, j + 1, m, n)):
            return True

        board[i][j] = word[index]
        return False

More from this blog

yesicbap

437 posts