-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_grid_search.py
81 lines (63 loc) · 1.95 KB
/
word_grid_search.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Given an m x n board of characters and a list of strings words,
return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells,
where adjacent cells are horizontally or vertically neighboring.
The same letter cell may not be used more than once in a word.
Examples:
Input:
board = [
["o","a","a","n"],
["e","t","a","e"],
["i","h","k","r"],
["i","f","l","v"]
]
words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Input:
board = [["a","b"],["c","d"]]
words = ["abcb"]
Output: []
"""
def find_words(board: list[list[str]], words: list[str]) -> list[str]:
trie = {}
result = []
rows, cols = len(board), len(board[0])
for word in words:
node = trie
for char in word:
if char not in node:
node[char] = {}
node = node[char]
node["#"] = word # end of word marker
def dfs(row, col, parent):
if (char := board[row][col]) not in parent:
return
node = parent[char] # get the next trie node
if "#" in node:
result.append(node["#"])
del node["#"]
board[row][col] = None # mark as visited
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
x, y = row + dx, col + dy
if 0 <= x < rows and 0 <= y < cols and board[x][y] is not None:
dfs(x, y, node)
board[row][col] = char # backtrack
if not node:
del parent[char]
for row in range(rows):
for col in range(cols):
dfs(row, col, trie)
return result
if __name__ == "__main__":
board = [
["o", "a", "a", "n"],
["e", "t", "a", "e"],
["i", "h", "k", "r"],
["i", "f", "l", "v"]
]
words = ["oath", "pea", "eat", "rain"]
print(find_words(board, words))
board = [["a", "b"], ["c", "d"]]
words = ["abcb"]
print(find_words(board, words))