-
Notifications
You must be signed in to change notification settings - Fork 0
/
212_WordSearchII.py
69 lines (59 loc) · 2.37 KB
/
212_WordSearchII.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
class TrieNode(object):
def __init__(self) -> None:
self.children = {} # key = letter, value = TrieNode
self.isWord = False
self.refs = 0 # number of times we can use this node
def addWord(self, word):
current = self
current.refs += 1
for c in word:
if c not in current.children:
current.children[c] = TrieNode()
current = current.children[c]
current.refs += 1
current.isWord = True
def removeWord(self, word):
current = self
current.refs -= 1
for c in word:
if c in current.children:
current = current.children[c]
current.refs -= 1
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
ROWS, COLS = len(board), len(board[0])
res = set()
usedSquares = []
# add words to the trie
root = TrieNode()
for word in words:
root.addWord(word)
# helper function
def dfs(r, c, usedSquares, currentWord, currentNode):
if (r < 0 or r >= ROWS or c < 0 or c >= COLS or
(r, c) in usedSquares or
board[r][c] not in currentNode.children or
currentNode.children[board[r][c]].refs < 1):
return None
letter = board[r][c]
currentNode = currentNode.children[letter]
currentWord += letter
if currentNode.isWord:
currentNode.isWord = False
res.add(currentWord)
root.removeWord(currentWord)
usedSquares.append((r, c))
# check neighbors for more words
dfs(r - 1, c, usedSquares, currentWord, currentNode) # up
dfs(r + 1, c, usedSquares, currentWord, currentNode) # down
dfs(r, c - 1, usedSquares, currentWord, currentNode) # left
dfs(r, c + 1, usedSquares, currentWord, currentNode) # right
# finished exploring so free to explore squares again
usedSquares.remove((r, c))
# start going through the board
for row in range(ROWS):
for col in range(COLS):
dfs(row, col, usedSquares, "", root)
if len(res) == len(words):
return res
return res