题目
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.
解题思路
DFS,使用Trie进行剪枝,一个优化是,把找到的单词对应的字典树的节点也删掉
代码
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
def dfs(x, y, root):
letter = board[x][y]
cur = root[letter]
word = cur.pop('#', False)
if word:
res.append(word)
board[x][y] = '*'
for dirx, diry in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
curx, cury = x + dirx, y + diry
if 0 <= curx < m and 0 <= cury < n and board[curx][cury] in cur:
dfs(curx, cury, cur)
board[x][y] = letter
if not cur:
root.pop(letter)
trie = {}
for word in words:
cur = trie
for letter in word:
cur = cur.setdefault(letter, {})
cur['#'] = word
m, n = len(board), len(board[0])
res = []
for i in range(m):
for j in range(n):
if board[i][j] in trie:
dfs(i, j, trie)
return res