-
Notifications
You must be signed in to change notification settings - Fork 481
/
0211.py
35 lines (31 loc) · 953 Bytes
/
0211.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
import collections
class WordDictionary:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = collections.defaultdict(list)
def addWord(self, word):
"""
Adds a word into the data structure.
:type word: str
:rtype: void
"""
self.root[len(word)].append(word)
def search(self, word):
"""
Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
:type word: str
:rtype: bool
"""
if not word:
return False
if "." not in word:
return word in self.root[len(word)]
for v in self.root[len(word)]:
for i, s in enumerate(word):
if s != v[i] and s != ".":
break
else:
return True
return False