-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_Search_II.py
45 lines (37 loc) · 1.37 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
class TrieNode:
def __init__(self):
self.isWord = False
self.children = {}
def addWord(self, word):
cur = self
for c in word:
if c not in cur.children:
cur.children[c] = TrieNode()
cur = cur.children[c]
cur.isWord = True
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
def dfs(x, y, visited, root, prefix):
if x < 0 or y < 0 or x == len(board[0]) or y == len(board): return False
if (x, y) in visited: return False
if board[y][x] not in root.children: return False
visited.add((x,y))
prefix += board[y][x]
root = root.children[board[y][x]]
if root.isWord:
res.add(prefix)
root.isWord = False
visited.add((x,y))
dfs(x+1, y, visited, root, prefix)
dfs(x-1, y, visited, root, prefix)
dfs(x, y+1, visited, root, prefix)
dfs(x, y-1, visited, root, prefix)
visited.remove((x,y))
root = TrieNode()
for w in words:
root.addWord(w)
res = set()
for y in range(len(board)):
for x in range(len(board[0])):
dfs(x, y, set(), root, "")
return res