text: # Given a 2D board and a list of words from the dictionary, find all words in the board.
  #
  # Each word must 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 in a word.
  #
  # Example:
  #
  # Input:
  # words = ["oath","pea","eat","rain"] and board =
  # [
  #   ['o','a','a','n'],
  #   ['e','t','a','e'],
  #   ['i','h','k','r'],
  #   ['i','f','l','v']
  # ]
  #
  # Output: ["eat","oath"]
  # Note:
  # You may assume that all inputs are consist of lowercase letters a-z.
  import collections
  class TrieNode():
      def __init__(self):
          self.children = collections.defaultdict(TrieNode)
          self.isWord = False
  
  
  class Trie():
      def __init__(self):
          self.root = TrieNode()
  
      def insert(self, word):
          node = self.root
          for w in word:
              node = node.children[w]
          node.isWord = True
  
      def search(self, word):
          node = self.root
          for w in word:
              node = node.children.get(w)
              if not node:
                  return False
          return node.isWord
  
  
  class Solution(object):
      def findWords(self, board, words):
          """
          :type board: List[List[str]]
          :type words: List[str]
          :rtype: List[str]
          """
          res = []
          trie = Trie()
          node = trie.root
          for word in words:
              trie.insert(word)
          for i in range(len(board)):
              for j in range(len(board[0])):
                  self.dfs(board, node, i, j, '', res)
          return res
  
      def dfs(self, board, node, i, j, path, res):
          if node.isWord:
              res.append(path)
              node.isWord = False
          if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]):
              return
          t = board[i][j]
          node = node.children.get(t)
          if not node:
              return
          board[i][j] = ''
          map(lambda x: self.dfs(board, node, x[0], x[1], path + t, res),
              [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)])
          board[i][j] = t