-
Notifications
You must be signed in to change notification settings - Fork 0
/
212_word_search_ii.py
59 lines (46 loc) · 1.5 KB
/
212_word_search_ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import List
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
trie = {}
end_word = "#"
tmp_word = "@"
for word in words:
children = trie
for c in word:
children = children.setdefault(c, {})
children[end_word] = end_word
row = len(board)
column = len(board[0])
res = []
def dfs(i: int, j: int, node: dict, s: str):
v = board[i][j]
if v not in node:
return
s += v
node = node[v]
if end_word in node:
res.append(s)
node.pop(end_word) # can only find once
board[i][j] = tmp_word
for (dx, dy) in ((0, 1), (0, -1), (1, 0), (-1, 0)):
x, y = i + dx, j + dy
if 0 <= x < row and 0 <= y < column and board[x][y] != tmp_word:
dfs(x, y, node, s)
board[i][j] = v
for r in range(row):
for c in range(column):
dfs(r, c, trie, "")
return res
def test():
words = ["oath", "pea", "eat", "rain", "ea"]
expect = ["ea", "eat", "oath"]
board = [
['o', 'a', 'a', 'n'],
['e', 't', 'a', 'e'],
['i', 'h', 'k', 'r'],
['i', 'f', 'l', 'v']
]
ans = Solution().findWords(board, words)
assert sorted(ans) == expect, f"expect: {expect}, got: {ans}"
if __name__ == "__main__":
test()