-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00212-word_search_ii.py
65 lines (47 loc) · 1.48 KB
/
00212-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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 212: Word Search Ii
# https://leetcode.com/problems/word-search-ii/
from typing import DefaultDict, List
class TrieNode:
def __init__(self):
self.children = DefaultDict(TrieNode)
self.word = None
def addWord(self, word):
cur = self
for c in word:
cur = cur.children[c]
cur.word = word
class Solution:
# SOLUTION
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
m, n = len(board), len(board[0])
DIR = [0, 1, 0, -1, 0]
trieNode = TrieNode()
result = []
for word in words:
trieNode.addWord(word)
def dfs(r, c, cur):
if r<0 or r==m or c<0 or c==n or board[r][c] not in cur.children: return
orgChar = board[r][c]
cur = cur.children[orgChar]
board[r][c] = '#'
if cur.word != None:
result.append(cur.word)
cur.word = None
for i in range(4): dfs(r + DIR[i], c + DIR[i + 1], cur)
board[r][c] = orgChar
for r in range(m):
for c in range(n):
dfs(r, c, trieNode)
return result
if __name__ == "__main__":
o = Solution()
# INPUT
board = [
["o","a","a","n"],
["e","t","a","e"],
["i","h","k","r"],
["i","f","l","v"]
]
words = ["oath","pea","eat","rain"]
# OUTPUT
print(o.findWords(board, words))