-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWordFilter.py
140 lines (119 loc) · 4.71 KB
/
WordFilter.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
import unittest
# class WordFilter(object):
# def __init__(self, words):
# self.words = words
# self.trie = Trie()
# def f(self, prefix, suffix):
# """
# :type prefix: str
# :type suffix: str
# :rtype: int
# """
# for idx, word in enumerate(self.words):
# self.trie.insertForward(word, idx)
# self.trie.insertBackward(word, idx)
# prefixFound = self.trie.find(prefix)
# suffixFound = self.trie.find(suffix)
# if prefixFound != [] and suffixFound != []:
# intersect = prefixFound & suffixFound
# return max(intersect)
# else:
# return -1
# class Trie:
# def __init__(self):
# self.root = TrieNode()
# def insert(self, info):
# word, idx = info
# root = self.root
# wordSoFar = ""
# for ch in word:
# wordSoFar += ch
# if ch not in root.children:
# root.children[ch] = TrieNode()
# root.children[ch].symbol = wordSoFar
# root.children[ch].idxs.append(idx)
# else:
# root = root.children[ch]
# def find(self, word):
# root = self.root
# ret = []
# for ch in word:
# if ch not in root.children:
# return []
# else:
# root = root.children[ch]
# ret.extend(root.idxs)
# print(ret)
# return set(ret)
# def insertForward(self, word, idx):
# self.insert((word, idx))
# def insertBackward(self, word, idx):
# self.insert((word[::-1], idx))
# class TrieNode:
# def __init__(self):
# self.symbol = "*"
# self.children = {}
# self.idxs = []
# # Your WordFilter object will be instantiated and called as such:
# # obj = WordFilter(words)
# # param_1 = obj.f(prefix,suffix)
class WordFilter(object):
def __init__(self, words):
self.prefix_dict = {}
self.suffix_dict = {}
self.cache = {}
# 0(N^2)time | 0(2N) space
for wordidx, word in enumerate(words):
idx = 0
while idx <= len(word):
prefix = word[:idx]
suffix = word[len(word)-idx:]
if prefix not in self.prefix_dict:
self.prefix_dict[prefix] = {wordidx}
else:
self.prefix_dict[prefix].add(wordidx)
if suffix not in self.suffix_dict:
self.suffix_dict[suffix] = {wordidx}
else:
self.suffix_dict[suffix].add(wordidx)
idx += 1
def f(self, prefix, suffix):
if (prefix, suffix) in self.cache:
return self.cache[(prefix, suffix)]
ans = -1
prefixFound = self.prefix_dict.get(prefix) # 0(1) lookup
suffixFound = self.suffix_dict.get(suffix)
if (prefixFound is not None) and (suffixFound is not None):
lans = list(prefixFound & suffixFound)
if len(lans) > 0:
ans = max(lans) # 0(max(N))
self.cache[(prefix, suffix)] = ans
return ans
class WordFilterTestCase(unittest.TestCase):
def testCreateHash(self):
w = WordFilter(["apple", "orange", "banana", "mango",
"return", "manufacture", "time"])
# self.assertDictEqual(
# w.cache, {'a#': [0, 0], '#e': [0, 0, 1], 'ap#': [0, 0], '#le': [0, 0],
# 'app#': [0, 0], '#ple': [0, 0], 'appl#': [0, 0],
# '#pple': [0, 0], 'apple#': [0, 0],
# '#apple': [0, 0], 'o#': [1, 1], 'or#': [1, 1],
# '#ge': [1, 1], 'ora#': [1, 1], '#nge': [1, 1],
# 'oran#': [1, 1], '#ange': [1, 1],
# 'orang#': [1, 1], '#range': [1, 1], 'orange#': [1, 1],
# '#orange': [1, 1], 'b#': [2, 2], '#a': [2, 2],
# 'ba#': [2, 2], '#na': [2, 2], 'ban#': [2, 2],
# '#ana': [2, 2], 'bana#': [2, 2], '#nana': [2, 2],
# 'banan#': [2, 2], '#anana': [2, 2],
# 'banana#': [2, 2], '#banana': [2, 2], 'm#': [3, 3], '#o': [3, 3], 'ma#': [3, 3],
# '#go': [3, 3], 'man#': [3, 3], '#ngo': [3, 3], 'mang#': [3, 3], '#ango': [3, 3],
# 'mango#': [3, 3], '#mango': [3, 3]}
# )
self.assertEqual(w.f("ti", "e"), 6)
self.assertEqual(w.f("re", "n"), 4)
self.assertEqual(w.f("manu", "ure"), 5)
self.assertEqual(w.f("ba", "pple"), -1)
self.assertEqual(w.f("app", "e"), 0)
self.assertEqual(w.f("time", "e"), 6)
if __name__ == "__main__":
unittest.main()