-
Notifications
You must be signed in to change notification settings - Fork 613
/
212.py
48 lines (44 loc) · 1.72 KB
/
212.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
class TrieNode(object):
def __init__(self):
self.value, self.links = None, [None]*26
class Trie(object):
def __init__(self):
self.root = TrieNode()
return
def insert(self, word):
if word:
curr = self.root
for ch in word:
offset = ord(ch)-ord('a')
if curr.links[offset] == None:
curr.links[offset] = TrieNode()
curr = curr.links[offset]
curr.value = word
return
class Solution(object):
def helper(self, x, y, board, trie_node, result):
if trie_node.value:
result.add(trie_node.value) # Look for other soln even if a soln is found. soln could a prefix of another soln.
for x1,y1 in ((x+1,y), (x-1,y), (x, y+1), (x, y-1)):
if 0<=x1<len(board) and 0<=y1<len(board[0]) and board[x1][y1] != -1 and trie_node.links[ord(board[x1][y1])-ord('a')]:
ch, board[x1][y1] = board[x1][y1], -1
self.helper(x1, y1, board, trie_node.links[ord(ch)-ord('a')], result)
board[x1][y1] = ch
return
def findWords(self, board, words):
"""
:type board: List[List[str]]
:type words: List[str]
:rtype: List[str]
"""
trie = Trie()
for word in words:
trie.insert(word)
result = set([])
for i in range(len(board)):
for j in range(len(board[0])):
if trie.root.links[ord(board[i][j])-ord('a')]:
ch, board[i][j] = board[i][j], -1
self.helper(i, j, board, trie.root.links[ord(ch)-ord('a')], result)
board[i][j] = ch
return [x for x in result]