LeetCode 212 [Word Search II]

原题

给出一个由小写字母组成的矩阵和一个字典。找出所有同时在字典和矩阵中出现的单词。一个单词可以从矩阵中的任意位置开始,可以向左/右/上/下四个相邻方向移动。

样例
给出矩阵:
doaf
agai
dcan
和字典:
{"dog", "dad", "dgdg", "can", "again"}
返回 {"dog", "dad", "can", "again"}

解题思路

  • 最直接但是会超时的方法 - 先把字典做成hashMap,对每一个点DFS,每增加一个字符就去hashMap查找是否存在。一共m*n个点,每次DFS复杂度是O(m*n),所以总的时间复杂度是O(m*n * m*n)
  • 可行解 - 把给出的字典变成Trie树,Trie树可以检查前缀,hashMap做不到。检查前缀的时候,如果发现某个前缀不存在,就可以不用继续DFS了,相当于剪枝
  • 注意:当不必要的check增多时,会导致TLE
# 超时
if x < 0 or x >= len(board) or y < 0 or y >= len(board[0]) or board[x][y] == 0 or cur_node == None:
# 免去两个不必要的check,不超时
if x < 0 or x >= len(board) or y < 0 or y >= len(board[0]):

完整代码

class TrieNode(object):
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.children = {}
        self.IsWord = False
        

class Trie(object):
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        """
        Inserts a word into the trie.
        :type word: str
        :rtype: void
        """
        node = self.root
        for letter in word:
            child = node.children.get(letter)
            if child is None:
                child = TrieNode()
                node.children[letter] = child
            node = child
        node.IsWord = True
        
class Solution(object):
    def __init__(self):
        self.result = []
    def findWords(self, board, words):
        """
        :type board: List[List[str]]
        :type words: List[str]
        :rtype: List[str]
        """
        trie = Trie()
        for word in words:
            trie.insert(word)
        
        for row_num in range(len(board)):
            for col_num in range(len(board[0])):
                self.search(board, row_num, col_num, trie.root, "")
                
        return self.result
                
    def search(self, board, x, y, cur_node, word):
        if cur_node.IsWord:
            self.result.append(word)
            cur_node.IsWord = False
        
        if not (x < 0 or x >= len(board) or y < 0 or y >= len(board[0])):
            temp = board[x][y]
            cur_node = cur_node.children.get(temp)
            if cur_node:
                board[x][y] = "#"
                self.search(board, x+1, y, cur_node, word+temp)
                self.search(board, x-1, y, cur_node, word+temp)
                self.search(board, x, y+1, cur_node, word+temp)
                self.search(board, x, y-1, cur_node, word+temp)
                board[x][y] = temp

你可能感兴趣的:(LeetCode 212 [Word Search II])