-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path212_Word_Search_II.py
More file actions
108 lines (94 loc) · 3.31 KB
/
212_Word_Search_II.py
File metadata and controls
108 lines (94 loc) · 3.31 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
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"]
"""
DIRECTIONS = [(1, 0),
(0, 1),
(-1, 0),
(0, -1)
]
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
words_set = set(words)
results = []
prefix_set = set()
for word in words:
for i in range(len(word)):
prefix_set.add(word[:i + 1])
for i in range(len(board)):
for j in range(len(board[0])):
self.dfs(board, i, j, board[i][j], words_set, prefix_set, set([(i, j)]), results)
return results
def dfs(self, board, x, y, word, words_set, prefix_set, visited, results):
if word not in prefix_set:
return
# NO Return -> Because there maybe words with same prefix like aa, aab, need to keep seaching
if word in words_set and word not in results: # check if word in results or use set for results
results.append(word)
for i, j in DIRECTIONS:
_x = x + i
_y = y + j
if 0 <= _x < len(board) and 0 <= _y < len(board[0]) and (_x, _y) not in visited:
visited.add((_x, _y))
self.dfs(board, _x, _y, word + board[_x][_y], words_set, prefix_set, visited, results)
visited.remove((_x, _y))
PATH = [(1, 0),
(0, 1),
(-1, 0),
(0, -1)
]
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
visited = set()
memo = {}
results = []
for i in range(len(words)):
char = words[i][0]
visited.clear()
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] == char:
if char in results or words[i] in results:
continue
visited.add((row, col))
if self.dfs_word(board, words[i][1:], (row, col), visited, memo):
memo[words[i]] = (row, col)
results.append(words[i])
visited.remove((row, col))
return results
def dfs_word(self, board, word, start_idx, visited, memo):
if len(word) == 0:
return True
char = word[0]
row = start_idx[0]
col = start_idx[1]
row_length = len(board)
col_length = len(board[0])
for i, j in PATH:
if 0 <= row + i < row_length and 0 <= col + j < col_length and board[row + i][col + j] == char:
if (row + i, col + j) in visited:
continue
if word in memo and (row + i, col + j) == memo[word]:
return True
visited.add((row + i, col + j))
if self.dfs_word(board, word[1:], (row + i, col + j), visited, memo):
memo[word] = True
return True
visited.remove((row + i, col + j))
return False
"""
CASE:
[["a","a"]]
["a"]
[["a","a"]]
["aa"]
[["a","b"],["c","d"]]
["ab","cb","ad","bd","ac","ca","da","bc","db","adcb","dabc","abb","acb"]
"""