-
Notifications
You must be signed in to change notification settings - Fork 17
/
word-search-ii.py
65 lines (47 loc) · 1.86 KB
/
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
59
60
61
62
63
64
65
from typing import List, Optional, Tuple, Dict, Iterator
class Trie:
def __init__(self, val: Optional[str]) -> None:
self.val = val
self.word: Optional[str] = None
self.children: Dict[str, Trie] = {}
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
root = Trie(None)
visited = [[False] * len(row) for row in board]
def add_word(root: Trie, word: str) -> None:
node = root
for char in word:
if char not in node.children:
node.children[char] = Trie(char)
node = node.children[char]
node.word = word
for word in words:
add_word(root, word)
rows, cols = len(board), len(board[0]) if board else 0
def neighbours(row, col) -> Iterator[Tuple[int, int]]:
for neigh_row, neigh_col in (
(row - 1, col),
(row + 1, col),
(row, col - 1),
(row, col + 1),
):
if (
0 <= neigh_row < rows
and 0 <= neigh_col < cols
and not visited[neigh_row][neigh_col]
):
yield neigh_row, neigh_col
result = set()
def dfs(row: int, col: int, node: Trie) -> None:
visited[row][col] = True
if board[row][col] in node.children:
cur_node = node.children[board[row][col]]
if cur_node.word:
result.add(cur_node.word)
for neigh_row, neigh_col in neighbours(row, col):
dfs(neigh_row, neigh_col, cur_node)
visited[row][col] = False
for row in range(rows):
for col in range(cols):
dfs(row, col, root)
return list(result)