-
Notifications
You must be signed in to change notification settings - Fork 2
/
211. Add and Search Word - Data structure design.py
141 lines (109 loc) · 3.52 KB
/
211. Add and Search Word - Data structure design.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
# 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.
# For 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.
# pass version need to focus on the init Trie dict get function
from collections import defaultdict
class TrieNode(object):
def __init__(self):
self.node = defaultdict()
self.isWord = False
def __repr__(self):
return repr(self.node)
class WordDictionary(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TrieNode()
def addWord(self, word):
"""
Adds a word into the data structure.
:type word: str
:rtype: void
"""
curr = self.root
for letter in word:
child = curr.node.get(letter)
if child is None:
child = TrieNode()
curr.node[letter] = child
curr = child
curr.isWord = True
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
"""
return self.find(self.root, word)
def find(self, trie, word):
if word == '':
return trie.isWord
if word[0] == '.':
for i in trie.node:
if self.find(trie.node[i], word[1:]):
return True
else:
child = trie.node.get(word[0])
if child:
return self.find(child, word[1:])
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
# my old way which MLE
from collections import defaultdict
class TrieNode(object):
def __init__(self):
self.node = defaultdict(TrieNode)
self.isWord = False
class WordDictionary(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TrieNode()
def addWord(self, word):
"""
Adds a word into the data structure.
:type word: str
:rtype: void
"""
curr = self.root
for char in word:
curr = curr.node[char]
curr.isWord = True
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
"""
return self.find(self.root, word)
def find(self, trie, word):
if word == '':
return trie.isWord
if word[0] == '.':
for i in trie.node:
if self.find(trie.node[i], word[1:]):
return True
else:
child = trie.node[word[0]]
return self.find(child, word[1:])
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)