-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path211-Add-and-Search-Word.py
More file actions
32 lines (22 loc) · 928 Bytes
/
211-Add-and-Search-Word.py
File metadata and controls
32 lines (22 loc) · 928 Bytes
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
class WordDictionary(object):
def __init__(self):
# Initialize a collection to store words
self.word_store = collections.defaultdict(list)
def addWord(self, word):
# Adds a word into word_store
# Classified by how long is the word
self.word_store[len(word)].append(word)
def search(self, word):
word_len = len(word)
if '.' not in word:
return word in self.word_store[len(word)]
else:
for item in self.word_store[word_len]:
# Comparison character one by one
for alpha in range(0,word_len):
#print alpha
if word[alpha] != "." and word[alpha] != item[alpha]:
break
if alpha == (word_len-1) :
return True
return False