Skip to content

Commit d0efb2a

Browse files
committed
feat: add solutions to lc problem: No.0966
No.0966.Vowel Spellchecker
1 parent 872a87c commit d0efb2a

File tree

6 files changed

+526
-2
lines changed

6 files changed

+526
-2
lines changed

solution/0900-0999/0966.Vowel Spellchecker/README.md

+185-1
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,206 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:哈希表**
71+
72+
遍历 `wordlist`,将单词按照大小写不敏感、元音不敏感的规则分别存入哈希表 `low``pat` 中,其中 `low` 的键为单词的小写形式,`pat` 的键为将单词的元音字母替换为 `*` 后的字符串,值为单词本身。用哈希表 `s` 存储 `wordlist` 中的单词。
73+
74+
遍历 `queries`,对于每个单词 `q`,如果 `q``s` 中,说明 `q``wordlist` 中,直接将 `q` 加入答案数组 `ans` 中;否则,如果 `q` 的小写形式在 `low` 中,说明 `q``wordlist` 中,且大小写不敏感,将 `low[q.lower()]` 加入答案数组 `ans` 中;否则,如果将 `q` 的元音字母替换为 `*` 后的字符串在 `pat` 中,说明 `q``wordlist` 中,且元音不敏感,将 `pat[f(q)]` 加入答案数组 `ans` 中;否则,说明 `q``wordlist` 中,且大小写和元音都不敏感,将空字符串加入答案数组 `ans` 中。
75+
76+
最后返回答案数组 `ans` 即可。
77+
78+
时间复杂度 $O(n+m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别为 `wordlist``queries` 的长度。
79+
7080
<!-- tabs:start -->
7181

7282
### **Python3**
7383

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

7686
```python
77-
87+
class Solution:
88+
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
89+
def f(w):
90+
t = []
91+
for c in w:
92+
t.append("*" if c in "aeiou" else c)
93+
return "".join(t)
94+
95+
s = set(wordlist)
96+
low, pat = {}, {}
97+
for w in wordlist:
98+
t = w.lower()
99+
low.setdefault(t, w)
100+
pat.setdefault(f(t), w)
101+
102+
ans = []
103+
for q in queries:
104+
if q in s:
105+
ans.append(q)
106+
continue
107+
q = q.lower()
108+
if q in low:
109+
ans.append(low[q])
110+
continue
111+
q = f(q)
112+
if q in pat:
113+
ans.append(pat[q])
114+
continue
115+
ans.append("")
116+
return ans
78117
```
79118

80119
### **Java**
81120

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

84123
```java
124+
class Solution {
125+
public String[] spellchecker(String[] wordlist, String[] queries) {
126+
Set<String> s = new HashSet<>();
127+
Map<String, String> low = new HashMap<>();
128+
Map<String, String> pat = new HashMap<>();
129+
for (String w : wordlist) {
130+
s.add(w);
131+
String t = w.toLowerCase();
132+
low.putIfAbsent(t, w);
133+
pat.putIfAbsent(f(t), w);
134+
}
135+
int m = queries.length;
136+
String[] ans = new String[m];
137+
for (int i = 0; i < m; ++i) {
138+
String q = queries[i];
139+
if (s.contains(q)) {
140+
ans[i] = q;
141+
continue;
142+
}
143+
q = q.toLowerCase();
144+
if (low.containsKey(q)) {
145+
ans[i] = low.get(q);
146+
continue;
147+
}
148+
q = f(q);
149+
if (pat.containsKey(q)) {
150+
ans[i] = pat.get(q);
151+
continue;
152+
}
153+
ans[i] = "";
154+
}
155+
return ans;
156+
}
157+
158+
private String f(String w) {
159+
char[] cs = w.toCharArray();
160+
for (int i = 0; i < cs.length; ++i) {
161+
char c = cs[i];
162+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
163+
cs[i] = '*';
164+
}
165+
}
166+
return String.valueOf(cs);
167+
}
168+
}
169+
```
170+
171+
### **C++**
172+
173+
```cpp
174+
class Solution {
175+
public:
176+
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
177+
unordered_set<string> s(wordlist.begin(), wordlist.end());
178+
unordered_map<string, string> low;
179+
unordered_map<string, string> pat;
180+
auto f = [](string& w) {
181+
string res;
182+
for (char& c : w) {
183+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
184+
res += '*';
185+
} else {
186+
res += c;
187+
}
188+
}
189+
return res;
190+
};
191+
for (auto& w : wordlist) {
192+
string t = w;
193+
transform(t.begin(), t.end(), t.begin(), ::tolower);
194+
if (!low.count(t)) {
195+
low[t] = w;
196+
}
197+
t = f(t);
198+
if (!pat.count(t)) {
199+
pat[t] = w;
200+
}
201+
}
202+
vector<string> ans;
203+
for (auto& q : queries) {
204+
if (s.count(q)) {
205+
ans.emplace_back(q);
206+
continue;
207+
}
208+
transform(q.begin(), q.end(), q.begin(), ::tolower);
209+
if (low.count(q)) {
210+
ans.emplace_back(low[q]);
211+
continue;
212+
}
213+
q = f(q);
214+
if (pat.count(q)) {
215+
ans.emplace_back(pat[q]);
216+
continue;
217+
}
218+
ans.emplace_back("");
219+
}
220+
return ans;
221+
}
222+
};
223+
```
85224
225+
### **Go**
226+
227+
```go
228+
func spellchecker(wordlist []string, queries []string) (ans []string) {
229+
s := map[string]bool{}
230+
low := map[string]string{}
231+
pat := map[string]string{}
232+
f := func(w string) string {
233+
res := []byte(w)
234+
for i := range res {
235+
if res[i] == 'a' || res[i] == 'e' || res[i] == 'i' || res[i] == 'o' || res[i] == 'u' {
236+
res[i] = '*'
237+
}
238+
}
239+
return string(res)
240+
}
241+
for _, w := range wordlist {
242+
s[w] = true
243+
t := strings.ToLower(w)
244+
if _, ok := low[t]; !ok {
245+
low[t] = w
246+
}
247+
if _, ok := pat[f(t)]; !ok {
248+
pat[f(t)] = w
249+
}
250+
}
251+
for _, q := range queries {
252+
if s[q] {
253+
ans = append(ans, q)
254+
continue
255+
}
256+
q = strings.ToLower(q)
257+
if s, ok := low[q]; ok {
258+
ans = append(ans, s)
259+
continue
260+
}
261+
q = f(q)
262+
if s, ok := pat[q]; ok {
263+
ans = append(ans, s)
264+
continue
265+
}
266+
ans = append(ans, "")
267+
}
268+
return
269+
}
86270
```
87271

88272
### **...**

solution/0900-0999/0966.Vowel Spellchecker/README_EN.md

+175-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,187 @@
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
65+
def f(w):
66+
t = []
67+
for c in w:
68+
t.append("*" if c in "aeiou" else c)
69+
return "".join(t)
70+
71+
s = set(wordlist)
72+
low, pat = {}, {}
73+
for w in wordlist:
74+
t = w.lower()
75+
low.setdefault(t, w)
76+
pat.setdefault(f(t), w)
77+
78+
ans = []
79+
for q in queries:
80+
if q in s:
81+
ans.append(q)
82+
continue
83+
q = q.lower()
84+
if q in low:
85+
ans.append(low[q])
86+
continue
87+
q = f(q)
88+
if q in pat:
89+
ans.append(pat[q])
90+
continue
91+
ans.append("")
92+
return ans
6493
```
6594

6695
### **Java**
6796

6897
```java
98+
class Solution {
99+
public String[] spellchecker(String[] wordlist, String[] queries) {
100+
Set<String> s = new HashSet<>();
101+
Map<String, String> low = new HashMap<>();
102+
Map<String, String> pat = new HashMap<>();
103+
for (String w : wordlist) {
104+
s.add(w);
105+
String t = w.toLowerCase();
106+
low.putIfAbsent(t, w);
107+
pat.putIfAbsent(f(t), w);
108+
}
109+
int m = queries.length;
110+
String[] ans = new String[m];
111+
for (int i = 0; i < m; ++i) {
112+
String q = queries[i];
113+
if (s.contains(q)) {
114+
ans[i] = q;
115+
continue;
116+
}
117+
q = q.toLowerCase();
118+
if (low.containsKey(q)) {
119+
ans[i] = low.get(q);
120+
continue;
121+
}
122+
q = f(q);
123+
if (pat.containsKey(q)) {
124+
ans[i] = pat.get(q);
125+
continue;
126+
}
127+
ans[i] = "";
128+
}
129+
return ans;
130+
}
131+
132+
private String f(String w) {
133+
char[] cs = w.toCharArray();
134+
for (int i = 0; i < cs.length; ++i) {
135+
char c = cs[i];
136+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
137+
cs[i] = '*';
138+
}
139+
}
140+
return String.valueOf(cs);
141+
}
142+
}
143+
```
144+
145+
### **C++**
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
151+
unordered_set<string> s(wordlist.begin(), wordlist.end());
152+
unordered_map<string, string> low;
153+
unordered_map<string, string> pat;
154+
auto f = [](string& w) {
155+
string res;
156+
for (char& c : w) {
157+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
158+
res += '*';
159+
} else {
160+
res += c;
161+
}
162+
}
163+
return res;
164+
};
165+
for (auto& w : wordlist) {
166+
string t = w;
167+
transform(t.begin(), t.end(), t.begin(), ::tolower);
168+
if (!low.count(t)) {
169+
low[t] = w;
170+
}
171+
t = f(t);
172+
if (!pat.count(t)) {
173+
pat[t] = w;
174+
}
175+
}
176+
vector<string> ans;
177+
for (auto& q : queries) {
178+
if (s.count(q)) {
179+
ans.emplace_back(q);
180+
continue;
181+
}
182+
transform(q.begin(), q.end(), q.begin(), ::tolower);
183+
if (low.count(q)) {
184+
ans.emplace_back(low[q]);
185+
continue;
186+
}
187+
q = f(q);
188+
if (pat.count(q)) {
189+
ans.emplace_back(pat[q]);
190+
continue;
191+
}
192+
ans.emplace_back("");
193+
}
194+
return ans;
195+
}
196+
};
197+
```
69198
199+
### **Go**
200+
201+
```go
202+
func spellchecker(wordlist []string, queries []string) (ans []string) {
203+
s := map[string]bool{}
204+
low := map[string]string{}
205+
pat := map[string]string{}
206+
f := func(w string) string {
207+
res := []byte(w)
208+
for i := range res {
209+
if res[i] == 'a' || res[i] == 'e' || res[i] == 'i' || res[i] == 'o' || res[i] == 'u' {
210+
res[i] = '*'
211+
}
212+
}
213+
return string(res)
214+
}
215+
for _, w := range wordlist {
216+
s[w] = true
217+
t := strings.ToLower(w)
218+
if _, ok := low[t]; !ok {
219+
low[t] = w
220+
}
221+
if _, ok := pat[f(t)]; !ok {
222+
pat[f(t)] = w
223+
}
224+
}
225+
for _, q := range queries {
226+
if s[q] {
227+
ans = append(ans, q)
228+
continue
229+
}
230+
q = strings.ToLower(q)
231+
if s, ok := low[q]; ok {
232+
ans = append(ans, s)
233+
continue
234+
}
235+
q = f(q)
236+
if s, ok := pat[q]; ok {
237+
ans = append(ans, s)
238+
continue
239+
}
240+
ans = append(ans, "")
241+
}
242+
return
243+
}
70244
```
71245

72246
### **...**

0 commit comments

Comments
 (0)