Skip to content

Commit 629366e

Browse files
committed
feat: add solutions to lc/lcof2 problem: No.0676,064
lc No.0676 & lcof2 No.064. Implement Magic Dictionary
1 parent 1d4a3e4 commit 629366e

File tree

11 files changed

+852
-21
lines changed

11 files changed

+852
-21
lines changed

lcof2/剑指 Offer II 064. 神奇的字典/README.md

+175
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,197 @@ magicDictionary.search("leetcoded"); // 返回 False
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
哈希表实现。
68+
6769
<!-- tabs:start -->
6870

6971
### **Python3**
7072

7173
<!-- 这里可写当前语言的特殊实现逻辑 -->
7274

7375
```python
76+
class MagicDictionary:
77+
78+
def __init__(self):
79+
"""
80+
Initialize your data structure here.
81+
"""
82+
83+
def _patterns(self, word):
84+
return [word[:i] + '*' + word[i + 1:] for i in range(len(word))]
85+
86+
def buildDict(self, dictionary: List[str]) -> None:
87+
self.words = set(dictionary)
88+
self.counter = collections.Counter(
89+
p for word in dictionary for p in self._patterns(word))
90+
91+
def search(self, searchWord: str) -> bool:
92+
for p in self._patterns(searchWord):
93+
if self.counter[p] > 1 or (self.counter[p] == 1 and searchWord not in self.words):
94+
return True
95+
return False
96+
7497

98+
# Your MagicDictionary object will be instantiated and called as such:
99+
# obj = MagicDictionary()
100+
# obj.buildDict(dictionary)
101+
# param_2 = obj.search(searchWord)
75102
```
76103

77104
### **Java**
78105

79106
<!-- 这里可写当前语言的特殊实现逻辑 -->
80107

81108
```java
109+
class MagicDictionary {
110+
private Set<String> words;
111+
private Map<String, Integer> counter;
112+
113+
/** Initialize your data structure here. */
114+
public MagicDictionary() {
115+
words = new HashSet<>();
116+
counter = new HashMap<>();
117+
}
118+
119+
public void buildDict(String[] dictionary) {
120+
for (String word : dictionary) {
121+
words.add(word);
122+
for (String p : patterns(word)) {
123+
counter.put(p, counter.getOrDefault(p, 0) + 1);
124+
}
125+
}
126+
}
127+
128+
public boolean search(String searchWord) {
129+
for (String p : patterns(searchWord)) {
130+
int cnt = counter.getOrDefault(p, 0);
131+
if (cnt > 1 || (cnt == 1 && !words.contains(searchWord))) {
132+
return true;
133+
}
134+
}
135+
return false;
136+
}
137+
138+
private List<String> patterns(String word) {
139+
List<String> res = new ArrayList<>();
140+
char[] chars = word.toCharArray();
141+
for (int i = 0; i < chars.length; ++i) {
142+
char c = chars[i];
143+
chars[i] = '*';
144+
res.add(new String(chars));
145+
chars[i] = c;
146+
}
147+
return res;
148+
}
149+
}
150+
151+
/**
152+
* Your MagicDictionary object will be instantiated and called as such:
153+
* MagicDictionary obj = new MagicDictionary();
154+
* obj.buildDict(dictionary);
155+
* boolean param_2 = obj.search(searchWord);
156+
*/
157+
```
158+
159+
### **C++**
160+
161+
```cpp
162+
class MagicDictionary {
163+
public:
164+
/** Initialize your data structure here. */
165+
MagicDictionary() {
166+
167+
}
168+
169+
void buildDict(vector<string> dictionary) {
170+
for (string word : dictionary)
171+
{
172+
words.insert(word);
173+
for (string p : patterns(word)) ++counter[p];
174+
}
175+
}
176+
177+
bool search(string searchWord) {
178+
for (string p : patterns(searchWord))
179+
{
180+
if (counter[p] > 1 || (counter[p] == 1 && !words.count(searchWord))) return true;
181+
}
182+
return false;
183+
}
184+
185+
private:
186+
unordered_set<string> words;
187+
unordered_map<string, int> counter;
188+
189+
vector<string> patterns(string word) {
190+
vector<string> res;
191+
for (int i = 0; i < word.size(); ++i)
192+
{
193+
char c = word[i];
194+
word[i] = '*';
195+
res.push_back(word);
196+
word[i] = c;
197+
}
198+
return res;
199+
}
200+
};
201+
202+
/**
203+
* Your MagicDictionary object will be instantiated and called as such:
204+
* MagicDictionary* obj = new MagicDictionary();
205+
* obj->buildDict(dictionary);
206+
* bool param_2 = obj->search(searchWord);
207+
*/
208+
```
209+
210+
### **Go**
211+
212+
```go
213+
type MagicDictionary struct {
214+
words map[string]bool
215+
counter map[string]int
216+
}
217+
218+
/** Initialize your data structure here. */
219+
func Constructor() MagicDictionary {
220+
return MagicDictionary{
221+
words: make(map[string]bool),
222+
counter: make(map[string]int),
223+
}
224+
}
225+
226+
func (this *MagicDictionary) BuildDict(dictionary []string) {
227+
for _, word := range dictionary {
228+
this.words[word] = true
229+
for _, p := range patterns(word) {
230+
this.counter[p]++
231+
}
232+
}
233+
}
234+
235+
func (this *MagicDictionary) Search(searchWord string) bool {
236+
for _, p := range patterns(searchWord) {
237+
if this.counter[p] > 1 || (this.counter[p] == 1 && !this.words[searchWord]) {
238+
return true
239+
}
240+
}
241+
return false
242+
}
243+
244+
func patterns(word string) []string {
245+
var res []string
246+
for i := 0; i < len(word); i++ {
247+
res = append(res, word[:i]+"."+word[i+1:])
248+
}
249+
return res
250+
}
82251
252+
/**
253+
* Your MagicDictionary object will be instantiated and called as such:
254+
* obj := Constructor();
255+
* obj.BuildDict(dictionary);
256+
* param_2 := obj.Search(searchWord);
257+
*/
83258
```
84259

85260
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class MagicDictionary {
2+
public:
3+
/** Initialize your data structure here. */
4+
MagicDictionary() {
5+
6+
}
7+
8+
void buildDict(vector<string> dictionary) {
9+
for (string word : dictionary)
10+
{
11+
words.insert(word);
12+
for (string p : patterns(word)) ++counter[p];
13+
}
14+
}
15+
16+
bool search(string searchWord) {
17+
for (string p : patterns(searchWord))
18+
{
19+
if (counter[p] > 1 || (counter[p] == 1 && !words.count(searchWord))) return true;
20+
}
21+
return false;
22+
}
23+
24+
private:
25+
unordered_set<string> words;
26+
unordered_map<string, int> counter;
27+
28+
vector<string> patterns(string word) {
29+
vector<string> res;
30+
for (int i = 0; i < word.size(); ++i)
31+
{
32+
char c = word[i];
33+
word[i] = '*';
34+
res.push_back(word);
35+
word[i] = c;
36+
}
37+
return res;
38+
}
39+
};
40+
41+
/**
42+
* Your MagicDictionary object will be instantiated and called as such:
43+
* MagicDictionary* obj = new MagicDictionary();
44+
* obj->buildDict(dictionary);
45+
* bool param_2 = obj->search(searchWord);
46+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
type MagicDictionary struct {
2+
words map[string]bool
3+
counter map[string]int
4+
}
5+
6+
/** Initialize your data structure here. */
7+
func Constructor() MagicDictionary {
8+
return MagicDictionary{
9+
words: make(map[string]bool),
10+
counter: make(map[string]int),
11+
}
12+
}
13+
14+
func (this *MagicDictionary) BuildDict(dictionary []string) {
15+
for _, word := range dictionary {
16+
this.words[word] = true
17+
for _, p := range patterns(word) {
18+
this.counter[p]++
19+
}
20+
}
21+
}
22+
23+
func (this *MagicDictionary) Search(searchWord string) bool {
24+
for _, p := range patterns(searchWord) {
25+
if this.counter[p] > 1 || (this.counter[p] == 1 && !this.words[searchWord]) {
26+
return true
27+
}
28+
}
29+
return false
30+
}
31+
32+
func patterns(word string) []string {
33+
var res []string
34+
for i := 0; i < len(word); i++ {
35+
res = append(res, word[:i]+"."+word[i+1:])
36+
}
37+
return res
38+
}
39+
40+
/**
41+
* Your MagicDictionary object will be instantiated and called as such:
42+
* obj := Constructor();
43+
* obj.BuildDict(dictionary);
44+
* param_2 := obj.Search(searchWord);
45+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class MagicDictionary {
2+
private Set<String> words;
3+
private Map<String, Integer> counter;
4+
5+
/** Initialize your data structure here. */
6+
public MagicDictionary() {
7+
words = new HashSet<>();
8+
counter = new HashMap<>();
9+
}
10+
11+
public void buildDict(String[] dictionary) {
12+
for (String word : dictionary) {
13+
words.add(word);
14+
for (String p : patterns(word)) {
15+
counter.put(p, counter.getOrDefault(p, 0) + 1);
16+
}
17+
}
18+
}
19+
20+
public boolean search(String searchWord) {
21+
for (String p : patterns(searchWord)) {
22+
int cnt = counter.getOrDefault(p, 0);
23+
if (cnt > 1 || (cnt == 1 && !words.contains(searchWord))) {
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
private List<String> patterns(String word) {
31+
List<String> res = new ArrayList<>();
32+
char[] chars = word.toCharArray();
33+
for (int i = 0; i < chars.length; ++i) {
34+
char c = chars[i];
35+
chars[i] = '*';
36+
res.add(new String(chars));
37+
chars[i] = c;
38+
}
39+
return res;
40+
}
41+
}
42+
43+
/**
44+
* Your MagicDictionary object will be instantiated and called as such:
45+
* MagicDictionary obj = new MagicDictionary();
46+
* obj.buildDict(dictionary);
47+
* boolean param_2 = obj.search(searchWord);
48+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class MagicDictionary:
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
8+
def _patterns(self, word):
9+
return [word[:i] + '*' + word[i + 1:] for i in range(len(word))]
10+
11+
def buildDict(self, dictionary: List[str]) -> None:
12+
self.words = set(dictionary)
13+
self.counter = collections.Counter(
14+
p for word in dictionary for p in self._patterns(word))
15+
16+
def search(self, searchWord: str) -> bool:
17+
for p in self._patterns(searchWord):
18+
if self.counter[p] > 1 or (self.counter[p] == 1 and searchWord not in self.words):
19+
return True
20+
return False
21+
22+
23+
# Your MagicDictionary object will be instantiated and called as such:
24+
# obj = MagicDictionary()
25+
# obj.buildDict(dictionary)
26+
# param_2 = obj.search(searchWord)

0 commit comments

Comments
 (0)