-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path211.AddandSearchWord-Datastructuredesign.py
72 lines (62 loc) · 2.23 KB
/
211.AddandSearchWord-Datastructuredesign.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
"""
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string
containing only letters a-z or .. A . means it can represent any one letter.
Example:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
"""
#Difficulty: Medium
#13 / 13 test cases passed.
#Runtime: 588 ms
#Memory Usage: 30.5 MB
#Runtime: 588 ms, faster than 12.54% of Python3 online submissions for Add and Search Word - Data structure design.
#Memory Usage: 30.5 MB, less than 9.69% of Python3 online submissions for Add and Search Word - Data structure design.
class WordDictionary:
def __init__(self):
"""
Initialize your data structure here.
"""
self.children = [None] * 26
self.is_word = False
def addWord(self, word: str) -> None:
"""
Adds a word into the data structure.
"""
current = self
for char in word:
index = ord(char) - ord('a')
if not current.children[index]:
current.children[index] = WordDictionary()
current = current.children[index]
current.is_word = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the data structure. A word could contain the
dot character '.' to represent any one letter.
"""
current = self
for i in range(len(word)):
index = ord(word[i]) - ord('a')
if word[i] == '.':
for char in current.children:
if char and char.search(word[i+1:]):
return True
return False
if not current.children[index]:
return False
current = current.children[index]
return current and current.is_word
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)