Skip to content

Commit c151b55

Browse files
authored
feat: add solutions to lc problems: No.0966,3227 (#4694)
1 parent 559703b commit c151b55

File tree

9 files changed

+423
-20
lines changed

9 files changed

+423
-20
lines changed

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

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ tags:
8787

8888
我们遍历 $\textit{wordlist}$,将单词按照大小写不敏感、元音不敏感的规则分别存入哈希表 $\textit{low}$ 和 $\textit{pat}$ 中,其中 $\textit{low}$ 的键为单词的小写形式,$\textit{pat}$ 的键为将单词的元音字母替换为 `*` 后的字符串,值为单词本身。用哈希表 $\textit{s}$ 存储 $\textit{wordlist}$ 中的单词。
8989

90-
遍历 $\textit{queries}$,对于每个单词 $\textit{q}$,如果 $\textit{q}$ 在 $\textit{s}$ 中,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,直接将 $\textit{q}$ 加入答案数组 $\textit{ans}$ 中;否则,如果 $\textit{q}$ 的小写形式在 $\textit{low}$ 中,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,且大小写不敏感,将 $\textit{low}[q.\text{lower}()]$ 加入答案数组 $\textit{ans}$ 中;否则,如果将 $\textit{q}$ 的元音字母替换为 `*` 后的字符串在 $\textit{pat}$ 中,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,且元音不敏感,将 $\textit{pat}[f(q)]$ 加入答案数组 $\textit{ans}$ 中;否则,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,且大小写和元音都不敏感,将空字符串加入答案数组 $\textit{ans}$ 中。
90+
遍历 $\textit{queries}$,对于每个单词 $\textit{q}$,如果 $\textit{q}$ 在 $\textit{s}$ 中,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,直接将 $\textit{q}$ 加入答案数组 $\textit{ans}$ 中。
91+
92+
否则,如果 $\textit{q}$ 的小写形式在 $\textit{low}$ 中,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,且大小写不敏感,将 $\textit{low}[q.\text{lower}()]$ 加入答案数组 $\textit{ans}$ 中。
93+
94+
否则,如果将 $\textit{q}$ 的元音字母替换为 `*` 后的字符串在 $\textit{pat}$ 中,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,且元音不敏感,将 $\textit{pat}[f(q)]$ 加入答案数组 $\textit{ans}$ 中。
95+
96+
否则,说明 $\textit{q}$ 在 $\textit{wordlist}$ 中,且大小写和元音都不敏感,将空字符串加入答案数组 $\textit{ans}$ 中。
9197

9298
最后返回答案数组 $\textit{ans}$ 即可。
9399

@@ -200,30 +206,30 @@ public:
200206
}
201207
return res;
202208
};
203-
for (auto& w : wordlist) {
209+
for (const auto& w : wordlist) {
204210
string t = w;
205211
transform(t.begin(), t.end(), t.begin(), ::tolower);
206-
if (!low.count(t)) {
212+
if (!low.contains(t)) {
207213
low[t] = w;
208214
}
209215
t = f(t);
210-
if (!pat.count(t)) {
216+
if (!pat.contains(t)) {
211217
pat[t] = w;
212218
}
213219
}
214220
vector<string> ans;
215221
for (auto& q : queries) {
216-
if (s.count(q)) {
222+
if (s.contains(q)) {
217223
ans.emplace_back(q);
218224
continue;
219225
}
220226
transform(q.begin(), q.end(), q.begin(), ::tolower);
221-
if (low.count(q)) {
227+
if (low.contains(q)) {
222228
ans.emplace_back(low[q]);
223229
continue;
224230
}
225231
q = f(q);
226-
if (pat.count(q)) {
232+
if (pat.contains(q)) {
227233
ans.emplace_back(pat[q]);
228234
continue;
229235
}
@@ -281,6 +287,113 @@ func spellchecker(wordlist []string, queries []string) (ans []string) {
281287
}
282288
```
283289

290+
#### TypeScript
291+
292+
```ts
293+
function spellchecker(wordlist: string[], queries: string[]): string[] {
294+
const s = new Set(wordlist);
295+
const low = new Map<string, string>();
296+
const pat = new Map<string, string>();
297+
298+
const f = (w: string): string => {
299+
let res = '';
300+
for (const c of w) {
301+
if ('aeiou'.includes(c)) {
302+
res += '*';
303+
} else {
304+
res += c;
305+
}
306+
}
307+
return res;
308+
};
309+
310+
for (const w of wordlist) {
311+
let t = w.toLowerCase();
312+
if (!low.has(t)) {
313+
low.set(t, w);
314+
}
315+
t = f(t);
316+
if (!pat.has(t)) {
317+
pat.set(t, w);
318+
}
319+
}
320+
321+
const ans: string[] = [];
322+
for (let q of queries) {
323+
if (s.has(q)) {
324+
ans.push(q);
325+
continue;
326+
}
327+
q = q.toLowerCase();
328+
if (low.has(q)) {
329+
ans.push(low.get(q)!);
330+
continue;
331+
}
332+
q = f(q);
333+
if (pat.has(q)) {
334+
ans.push(pat.get(q)!);
335+
continue;
336+
}
337+
ans.push('');
338+
}
339+
return ans;
340+
}
341+
```
342+
343+
#### Rust
344+
345+
```rust
346+
use std::collections::{HashSet, HashMap};
347+
348+
impl Solution {
349+
pub fn spellchecker(wordlist: Vec<String>, queries: Vec<String>) -> Vec<String> {
350+
let s: HashSet<String> = wordlist.iter().cloned().collect();
351+
let mut low: HashMap<String, String> = HashMap::new();
352+
let mut pat: HashMap<String, String> = HashMap::new();
353+
354+
let f = |w: &str| -> String {
355+
w.chars()
356+
.map(|c| match c {
357+
'a' | 'e' | 'i' | 'o' | 'u' => '*',
358+
_ => c,
359+
})
360+
.collect()
361+
};
362+
363+
for w in &wordlist {
364+
let mut t = w.to_lowercase();
365+
if !low.contains_key(&t) {
366+
low.insert(t.clone(), w.clone());
367+
}
368+
t = f(&t);
369+
if !pat.contains_key(&t) {
370+
pat.insert(t.clone(), w.clone());
371+
}
372+
}
373+
374+
let mut ans: Vec<String> = Vec::new();
375+
for query in queries {
376+
if s.contains(&query) {
377+
ans.push(query);
378+
continue;
379+
}
380+
let mut q = query.to_lowercase();
381+
if let Some(v) = low.get(&q) {
382+
ans.push(v.clone());
383+
continue;
384+
}
385+
q = f(&q);
386+
if let Some(v) = pat.get(&q) {
387+
ans.push(v.clone());
388+
continue;
389+
}
390+
ans.push("".to_string());
391+
}
392+
ans
393+
}
394+
}
395+
```
396+
284397
<!-- tabs:end -->
285398

286399
<!-- solution:end -->

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

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,30 +192,30 @@ public:
192192
}
193193
return res;
194194
};
195-
for (auto& w : wordlist) {
195+
for (const auto& w : wordlist) {
196196
string t = w;
197197
transform(t.begin(), t.end(), t.begin(), ::tolower);
198-
if (!low.count(t)) {
198+
if (!low.contains(t)) {
199199
low[t] = w;
200200
}
201201
t = f(t);
202-
if (!pat.count(t)) {
202+
if (!pat.contains(t)) {
203203
pat[t] = w;
204204
}
205205
}
206206
vector<string> ans;
207207
for (auto& q : queries) {
208-
if (s.count(q)) {
208+
if (s.contains(q)) {
209209
ans.emplace_back(q);
210210
continue;
211211
}
212212
transform(q.begin(), q.end(), q.begin(), ::tolower);
213-
if (low.count(q)) {
213+
if (low.contains(q)) {
214214
ans.emplace_back(low[q]);
215215
continue;
216216
}
217217
q = f(q);
218-
if (pat.count(q)) {
218+
if (pat.contains(q)) {
219219
ans.emplace_back(pat[q]);
220220
continue;
221221
}
@@ -273,6 +273,113 @@ func spellchecker(wordlist []string, queries []string) (ans []string) {
273273
}
274274
```
275275

276+
#### TypeScript
277+
278+
```ts
279+
function spellchecker(wordlist: string[], queries: string[]): string[] {
280+
const s = new Set(wordlist);
281+
const low = new Map<string, string>();
282+
const pat = new Map<string, string>();
283+
284+
const f = (w: string): string => {
285+
let res = '';
286+
for (const c of w) {
287+
if ('aeiou'.includes(c)) {
288+
res += '*';
289+
} else {
290+
res += c;
291+
}
292+
}
293+
return res;
294+
};
295+
296+
for (const w of wordlist) {
297+
let t = w.toLowerCase();
298+
if (!low.has(t)) {
299+
low.set(t, w);
300+
}
301+
t = f(t);
302+
if (!pat.has(t)) {
303+
pat.set(t, w);
304+
}
305+
}
306+
307+
const ans: string[] = [];
308+
for (let q of queries) {
309+
if (s.has(q)) {
310+
ans.push(q);
311+
continue;
312+
}
313+
q = q.toLowerCase();
314+
if (low.has(q)) {
315+
ans.push(low.get(q)!);
316+
continue;
317+
}
318+
q = f(q);
319+
if (pat.has(q)) {
320+
ans.push(pat.get(q)!);
321+
continue;
322+
}
323+
ans.push('');
324+
}
325+
return ans;
326+
}
327+
```
328+
329+
#### Rust
330+
331+
```rust
332+
use std::collections::{HashSet, HashMap};
333+
334+
impl Solution {
335+
pub fn spellchecker(wordlist: Vec<String>, queries: Vec<String>) -> Vec<String> {
336+
let s: HashSet<String> = wordlist.iter().cloned().collect();
337+
let mut low: HashMap<String, String> = HashMap::new();
338+
let mut pat: HashMap<String, String> = HashMap::new();
339+
340+
let f = |w: &str| -> String {
341+
w.chars()
342+
.map(|c| match c {
343+
'a' | 'e' | 'i' | 'o' | 'u' => '*',
344+
_ => c,
345+
})
346+
.collect()
347+
};
348+
349+
for w in &wordlist {
350+
let mut t = w.to_lowercase();
351+
if !low.contains_key(&t) {
352+
low.insert(t.clone(), w.clone());
353+
}
354+
t = f(&t);
355+
if !pat.contains_key(&t) {
356+
pat.insert(t.clone(), w.clone());
357+
}
358+
}
359+
360+
let mut ans: Vec<String> = Vec::new();
361+
for query in queries {
362+
if s.contains(&query) {
363+
ans.push(query);
364+
continue;
365+
}
366+
let mut q = query.to_lowercase();
367+
if let Some(v) = low.get(&q) {
368+
ans.push(v.clone());
369+
continue;
370+
}
371+
q = f(&q);
372+
if let Some(v) = pat.get(&q) {
373+
ans.push(v.clone());
374+
continue;
375+
}
376+
ans.push("".to_string());
377+
}
378+
ans
379+
}
380+
}
381+
```
382+
276383
<!-- tabs:end -->
277384

278385
<!-- solution:end -->

solution/0900-0999/0966.Vowel Spellchecker/Solution.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,35 @@ class Solution {
1515
}
1616
return res;
1717
};
18-
for (auto& w : wordlist) {
18+
for (const auto& w : wordlist) {
1919
string t = w;
2020
transform(t.begin(), t.end(), t.begin(), ::tolower);
21-
if (!low.count(t)) {
21+
if (!low.contains(t)) {
2222
low[t] = w;
2323
}
2424
t = f(t);
25-
if (!pat.count(t)) {
25+
if (!pat.contains(t)) {
2626
pat[t] = w;
2727
}
2828
}
2929
vector<string> ans;
3030
for (auto& q : queries) {
31-
if (s.count(q)) {
31+
if (s.contains(q)) {
3232
ans.emplace_back(q);
3333
continue;
3434
}
3535
transform(q.begin(), q.end(), q.begin(), ::tolower);
36-
if (low.count(q)) {
36+
if (low.contains(q)) {
3737
ans.emplace_back(low[q]);
3838
continue;
3939
}
4040
q = f(q);
41-
if (pat.count(q)) {
41+
if (pat.contains(q)) {
4242
ans.emplace_back(pat[q]);
4343
continue;
4444
}
4545
ans.emplace_back("");
4646
}
4747
return ans;
4848
}
49-
};
49+
};

0 commit comments

Comments
 (0)