Description
給定一個二維網格 board 和一個字典中的單詞列表 words,找出所有同時在二維網格和字典中出現的單詞。
單詞必須按照字母順序,通過相鄰的單元格內的字母構成,其中“相鄰”單元格是那些水平相鄰或垂直相鄰的單元格。同一個單元格內的字母在一個單詞中不允許被重複使用。
示例:
輸入:
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
輸出: ["eat","oath"]
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/word-search-ii
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
Solution
題解
我們把所有單詞構造成前綴樹再遍歷 board 用DFS分別在前綴樹上跑。
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
trie = {}
for word in words:
t = trie
for w in word:
if w not in t: t[w]={}
t = t[w]
# t = t.setdefault(w, {}) 這樣寫不太直觀
t["end"] = 1
# print(trie)
res = []
row = len(board)
col = len(board[0])
def dfs(i, j, trie, s):
#print(i, j, trie, s)
c = board[i][j]
if c not in trie: return
trie = trie[c]
if "end" in trie and trie["end"] == 1:
res.append(s + c)
trie["end"] = 0 # 防止重複數組加入
board[i][j] = "#"
for x, y in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
tmp_i = x + i
tmp_j = y + j
if 0 <= tmp_i < row and 0 <= tmp_j < col and board[tmp_i][tmp_j] != "#":
dfs(tmp_i, tmp_j, trie, s + c)
board[i][j] = c
for i in range(row):
for j in range(col):
dfs(i, j, trie, "")
return res