Word Search

Question

Given a 2D board and a word, find if the word exists in the grid.

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

For example, Given board =

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

word = "ABCCED", -> returns true, word = "SEE", -> returns true, word = "ABCB", -> returns false.

Tags

  • DFS
  • Graph

Thought

Use the DFS search algorithm here.

Code

class Solution(object):
    def dfs(self, board, location, visited, ch_list):
        if len(ch_list) == 0:
            return True
        x, y = location
        tmpSet = {(x, y)}
        # up
        if x - 1 >= 0 and (x - 1, y) not in visited and board[x - 1][y] == ch_list[0]:
            if self.dfs(board, (x - 1, y), visited | tmpSet, ch_list[1:]):
                return True
        # down
        if x + 1 < len(board) and (x + 1, y) not in visited and board[x + 1][y] == ch_list[0]:
            if self.dfs(board, (x + 1, y), visited | tmpSet, ch_list[1:]):
                return True
        # left
        if y - 1 >= 0 and (x, y - 1) not in visited and board[x][y - 1] == ch_list[0]:
            if self.dfs(board, (x, y - 1), visited | tmpSet, ch_list[1:]):
                return True
        # right
        if y + 1 < len(board[0]) and (x, y + 1) not in visited and board[x][y + 1] == ch_list[0]:
            if self.dfs(board, (x, y + 1), visited | tmpSet, ch_list[1:]):
                return True

        return False



    def exist(self, board, word):
        """
        :type board: List[List[str]]
        :type word: str
        :rtype: bool
        """
        m, n = len(board), len(board[0])
        for i in xrange(m):
            for j in xrange(n):
                if board[i][j] == word[0]:
                    word_list = list(word)
                    visited = set()
                    location = (i, j)
                    if self.dfs(board, location, visited, word_list[1:]):
                        return True
        return False

results matching ""

    No results matching ""