-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc_0212_word_search_ii.py
211 lines (167 loc) · 6.45 KB
/
lc_0212_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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""212. Word Search II
https://leetcode.com/problems/word-search-ii/
---
Submitted three times:
> Runtime: 40 ms, faster than 99.95% of Python3 online submissions for Word Search II.
> Runtime: 44 ms, faster than 99.86% of Python3 online submissions for Word Search II.
> Runtime: 44 ms, faster than 99.86% of Python3 online submissions for Word Search II.
> Memory Usage: 15.1 MB, less than 96.19% of Python3 online submissions for Word Search II.
We use two heuristics before the actual DFS. Overall there are following steps:
1. Only consider the words that can be composed using this multiset of letters.
2. Perform a naive BFS first to solve a simpler problem where the letters can be reused.
3. Check that the solution works for our problem by the DFS from the end of the word.
As additional optimizations we use the following tricks:
1. Board positions are saved as small integers.
2. The BFS is performed by iteratively walking a trie.
"""
from collections import Counter, defaultdict
from functools import cache
from itertools import chain
from typing import List
from unittest import TestCase, main
class Trie:
def __init__(self, parent=None):
def trie_factory():
return defaultdict(lambda: Trie(parent=self))
self.parent = parent
self.root = trie_factory()
self.empty_value = None
def subtrie(self, ch, create=False):
return self.root[ch] if ch in self.root or create else None
def findtrie(self, prefix, create=False):
trie = self
for c in prefix:
trie = trie.subtrie(c, create)
if trie is None:
break
return trie
def add(self, word, value=None):
self.findtrie(word, create=True).empty_value = word if value is None else value
def query(self, word):
trie = self.findtrie(word)
return trie and trie.empty_value
def items(self):
return (
self.root.items()
if self.empty_value is None
else chain((("", self.empty_value),), self.root.items())
)
def __str__(self):
parts = [f"{ch}: {str(subtrie)}" for ch, subtrie in self.root.items()]
if self.empty_value is not None:
parts = [self.empty_value] + parts
return "<" + ", ".join(parts) + ">"
def __len__(self):
return (self.empty_value is not None) + sum(
len(sub) for sub in self.root.values()
)
def __bool__(self):
return len(self) != 0
def group_by_value(it, into=set, op=set.add):
answer = defaultdict(into)
for k, v in it:
op(answer[v], k) # PyCharm is not smart enough and displays a warning here.
return answer
# noinspection PyMethodMayBeStatic
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
m, n = len(board), len(board[0])
B = max(64, n + 2)
def g_enumerate():
"""Encode board positions into small numbers"""
for y, row in enumerate(board):
for x, val in enumerate(row):
yield B * y + x, val
@cache
def neighbors(p):
"""List of neighboring cells"""
return p + 1, p - 1, p + B, p - B
grid = dict(g_enumerate())
grid_by_ch = group_by_value(grid.items())
word_trie = Trie()
for w in words:
# Only keep words that are possible
if len(w) <= m * n and all(
v <= len(grid_by_ch[ch]) for ch, v in Counter(w).items()
):
word_trie.add(w, w)
def naive_bfs(trie=word_trie, cur=None):
cur_n = {q for p in cur for q in neighbors(p)} if cur else set(grid)
for ch, value in trie.items():
if not ch:
yield cur, value
elif next_ := cur_n & grid_by_ch[ch]:
yield from naive_bfs(value, next_)
def check_word(choices, word):
def dfs(p, pos, taken):
return any(
not pos or dfs(q, pos - 1, taken | {p})
for q in grid_by_ch[word[pos]].intersection(neighbors(p)) - taken
)
return any(dfs(p, len(word) - 2, set()) for p in choices)
# for choices, word in naive_bfs():
# print(choices, word, check_word(choices, word))
return [
word
for choices, word in naive_bfs()
if len(word) == 1 or check_word(choices, word)
]
# noinspection PyMethodMayBeStatic
class AuxiliaryTestCase(TestCase):
def test_trie(self):
t = Trie()
assert not t
assert not t.findtrie("wo", create=False)
t.add("wow", "wow")
t.add("won", "won")
assert t
assert len(t) == 2
assert len(t.findtrie("wo", create=False)) == 2
assert len(t.findtrie("won", create=False)) == 1
assert t.query("won") == "won"
assert t.query("wow") == "wow"
assert not t.query("wo") and not t.query("wont")
assert [("w", t.findtrie("w"))] == list(t.items())
assert [("", "won")] == list(t.findtrie("won").items())
def test_group(self):
assert group_by_value([("a", 1), ("b", 2), ("c", 1)]) == {
1: {"a", "c"},
2: {"b"},
}
class SolutionTestCase(TestCase):
def setUp(self) -> None:
self.f = Solution().findWords
def test_simple(self):
"""Check some simple cases"""
assert self.f([["X"]], ["X"]) == ["X"]
assert self.f([["X"]], ["Y"]) == []
def test_examples(self):
"""From the problem description"""
assert set(
self.f(
[
["o", "a", "a", "n"],
["e", "t", "a", "e"],
["i", "h", "k", "r"],
["i", "f", "l", "v"],
],
words=["oath", "pea", "eat", "rain"],
)
) == {"eat", "oath"}
assert self.f([["a", "b"], ["c", "d"]], words=["abcb"]) == []
def test_crosses(self):
"""Here the word can only be found on board if we take the letter 'a' twice."""
assert (
self.f(
[
["o", "b", "a", "n"],
["e", "t", "a", "e"],
["i", "h", "k", "r"],
["i", "f", "l", "v"],
],
words=["kata"],
)
== []
)
if __name__ == "__main__":
main()