diff --git a/solution/0900-0999/0966.Vowel Spellchecker/README.md b/solution/0900-0999/0966.Vowel Spellchecker/README.md index 5e8bbf32889bf..9ecba0b95547f 100644 --- a/solution/0900-0999/0966.Vowel Spellchecker/README.md +++ b/solution/0900-0999/0966.Vowel Spellchecker/README.md @@ -87,7 +87,13 @@ tags: 我们遍历 $\textit{wordlist}$,将单词按照大小写不敏感、元音不敏感的规则分别存入哈希表 $\textit{low}$ 和 $\textit{pat}$ 中,其中 $\textit{low}$ 的键为单词的小写形式,$\textit{pat}$ 的键为将单词的元音字母替换为 `*` 后的字符串,值为单词本身。用哈希表 $\textit{s}$ 存储 $\textit{wordlist}$ 中的单词。 -遍历 $\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}$ 中。 +遍历 $\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}$ 中。 最后返回答案数组 $\textit{ans}$ 即可。 @@ -200,30 +206,30 @@ public: } return res; }; - for (auto& w : wordlist) { + for (const auto& w : wordlist) { string t = w; transform(t.begin(), t.end(), t.begin(), ::tolower); - if (!low.count(t)) { + if (!low.contains(t)) { low[t] = w; } t = f(t); - if (!pat.count(t)) { + if (!pat.contains(t)) { pat[t] = w; } } vector ans; for (auto& q : queries) { - if (s.count(q)) { + if (s.contains(q)) { ans.emplace_back(q); continue; } transform(q.begin(), q.end(), q.begin(), ::tolower); - if (low.count(q)) { + if (low.contains(q)) { ans.emplace_back(low[q]); continue; } q = f(q); - if (pat.count(q)) { + if (pat.contains(q)) { ans.emplace_back(pat[q]); continue; } @@ -281,6 +287,113 @@ func spellchecker(wordlist []string, queries []string) (ans []string) { } ``` +#### TypeScript + +```ts +function spellchecker(wordlist: string[], queries: string[]): string[] { + const s = new Set(wordlist); + const low = new Map(); + const pat = new Map(); + + const f = (w: string): string => { + let res = ''; + for (const c of w) { + if ('aeiou'.includes(c)) { + res += '*'; + } else { + res += c; + } + } + return res; + }; + + for (const w of wordlist) { + let t = w.toLowerCase(); + if (!low.has(t)) { + low.set(t, w); + } + t = f(t); + if (!pat.has(t)) { + pat.set(t, w); + } + } + + const ans: string[] = []; + for (let q of queries) { + if (s.has(q)) { + ans.push(q); + continue; + } + q = q.toLowerCase(); + if (low.has(q)) { + ans.push(low.get(q)!); + continue; + } + q = f(q); + if (pat.has(q)) { + ans.push(pat.get(q)!); + continue; + } + ans.push(''); + } + return ans; +} +``` + +#### Rust + +```rust +use std::collections::{HashSet, HashMap}; + +impl Solution { + pub fn spellchecker(wordlist: Vec, queries: Vec) -> Vec { + let s: HashSet = wordlist.iter().cloned().collect(); + let mut low: HashMap = HashMap::new(); + let mut pat: HashMap = HashMap::new(); + + let f = |w: &str| -> String { + w.chars() + .map(|c| match c { + 'a' | 'e' | 'i' | 'o' | 'u' => '*', + _ => c, + }) + .collect() + }; + + for w in &wordlist { + let mut t = w.to_lowercase(); + if !low.contains_key(&t) { + low.insert(t.clone(), w.clone()); + } + t = f(&t); + if !pat.contains_key(&t) { + pat.insert(t.clone(), w.clone()); + } + } + + let mut ans: Vec = Vec::new(); + for query in queries { + if s.contains(&query) { + ans.push(query); + continue; + } + let mut q = query.to_lowercase(); + if let Some(v) = low.get(&q) { + ans.push(v.clone()); + continue; + } + q = f(&q); + if let Some(v) = pat.get(&q) { + ans.push(v.clone()); + continue; + } + ans.push("".to_string()); + } + ans + } +} +``` + diff --git a/solution/0900-0999/0966.Vowel Spellchecker/README_EN.md b/solution/0900-0999/0966.Vowel Spellchecker/README_EN.md index 49c4ba73b8b30..5f028a7b41316 100644 --- a/solution/0900-0999/0966.Vowel Spellchecker/README_EN.md +++ b/solution/0900-0999/0966.Vowel Spellchecker/README_EN.md @@ -192,30 +192,30 @@ public: } return res; }; - for (auto& w : wordlist) { + for (const auto& w : wordlist) { string t = w; transform(t.begin(), t.end(), t.begin(), ::tolower); - if (!low.count(t)) { + if (!low.contains(t)) { low[t] = w; } t = f(t); - if (!pat.count(t)) { + if (!pat.contains(t)) { pat[t] = w; } } vector ans; for (auto& q : queries) { - if (s.count(q)) { + if (s.contains(q)) { ans.emplace_back(q); continue; } transform(q.begin(), q.end(), q.begin(), ::tolower); - if (low.count(q)) { + if (low.contains(q)) { ans.emplace_back(low[q]); continue; } q = f(q); - if (pat.count(q)) { + if (pat.contains(q)) { ans.emplace_back(pat[q]); continue; } @@ -273,6 +273,113 @@ func spellchecker(wordlist []string, queries []string) (ans []string) { } ``` +#### TypeScript + +```ts +function spellchecker(wordlist: string[], queries: string[]): string[] { + const s = new Set(wordlist); + const low = new Map(); + const pat = new Map(); + + const f = (w: string): string => { + let res = ''; + for (const c of w) { + if ('aeiou'.includes(c)) { + res += '*'; + } else { + res += c; + } + } + return res; + }; + + for (const w of wordlist) { + let t = w.toLowerCase(); + if (!low.has(t)) { + low.set(t, w); + } + t = f(t); + if (!pat.has(t)) { + pat.set(t, w); + } + } + + const ans: string[] = []; + for (let q of queries) { + if (s.has(q)) { + ans.push(q); + continue; + } + q = q.toLowerCase(); + if (low.has(q)) { + ans.push(low.get(q)!); + continue; + } + q = f(q); + if (pat.has(q)) { + ans.push(pat.get(q)!); + continue; + } + ans.push(''); + } + return ans; +} +``` + +#### Rust + +```rust +use std::collections::{HashSet, HashMap}; + +impl Solution { + pub fn spellchecker(wordlist: Vec, queries: Vec) -> Vec { + let s: HashSet = wordlist.iter().cloned().collect(); + let mut low: HashMap = HashMap::new(); + let mut pat: HashMap = HashMap::new(); + + let f = |w: &str| -> String { + w.chars() + .map(|c| match c { + 'a' | 'e' | 'i' | 'o' | 'u' => '*', + _ => c, + }) + .collect() + }; + + for w in &wordlist { + let mut t = w.to_lowercase(); + if !low.contains_key(&t) { + low.insert(t.clone(), w.clone()); + } + t = f(&t); + if !pat.contains_key(&t) { + pat.insert(t.clone(), w.clone()); + } + } + + let mut ans: Vec = Vec::new(); + for query in queries { + if s.contains(&query) { + ans.push(query); + continue; + } + let mut q = query.to_lowercase(); + if let Some(v) = low.get(&q) { + ans.push(v.clone()); + continue; + } + q = f(&q); + if let Some(v) = pat.get(&q) { + ans.push(v.clone()); + continue; + } + ans.push("".to_string()); + } + ans + } +} +``` + diff --git a/solution/0900-0999/0966.Vowel Spellchecker/Solution.cpp b/solution/0900-0999/0966.Vowel Spellchecker/Solution.cpp index c983eaca2fa61..eefa4a3f297d6 100644 --- a/solution/0900-0999/0966.Vowel Spellchecker/Solution.cpp +++ b/solution/0900-0999/0966.Vowel Spellchecker/Solution.cpp @@ -15,30 +15,30 @@ class Solution { } return res; }; - for (auto& w : wordlist) { + for (const auto& w : wordlist) { string t = w; transform(t.begin(), t.end(), t.begin(), ::tolower); - if (!low.count(t)) { + if (!low.contains(t)) { low[t] = w; } t = f(t); - if (!pat.count(t)) { + if (!pat.contains(t)) { pat[t] = w; } } vector ans; for (auto& q : queries) { - if (s.count(q)) { + if (s.contains(q)) { ans.emplace_back(q); continue; } transform(q.begin(), q.end(), q.begin(), ::tolower); - if (low.count(q)) { + if (low.contains(q)) { ans.emplace_back(low[q]); continue; } q = f(q); - if (pat.count(q)) { + if (pat.contains(q)) { ans.emplace_back(pat[q]); continue; } @@ -46,4 +46,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0900-0999/0966.Vowel Spellchecker/Solution.rs b/solution/0900-0999/0966.Vowel Spellchecker/Solution.rs new file mode 100644 index 0000000000000..5ba1393922d3a --- /dev/null +++ b/solution/0900-0999/0966.Vowel Spellchecker/Solution.rs @@ -0,0 +1,49 @@ +use std::collections::{HashMap, HashSet}; + +impl Solution { + pub fn spellchecker(wordlist: Vec, queries: Vec) -> Vec { + let s: HashSet = wordlist.iter().cloned().collect(); + let mut low: HashMap = HashMap::new(); + let mut pat: HashMap = HashMap::new(); + + let f = |w: &str| -> String { + w.chars() + .map(|c| match c { + 'a' | 'e' | 'i' | 'o' | 'u' => '*', + _ => c, + }) + .collect() + }; + + for w in &wordlist { + let mut t = w.to_lowercase(); + if !low.contains_key(&t) { + low.insert(t.clone(), w.clone()); + } + t = f(&t); + if !pat.contains_key(&t) { + pat.insert(t.clone(), w.clone()); + } + } + + let mut ans: Vec = Vec::new(); + for query in queries { + if s.contains(&query) { + ans.push(query); + continue; + } + let mut q = query.to_lowercase(); + if let Some(v) = low.get(&q) { + ans.push(v.clone()); + continue; + } + q = f(&q); + if let Some(v) = pat.get(&q) { + ans.push(v.clone()); + continue; + } + ans.push("".to_string()); + } + ans + } +} diff --git a/solution/0900-0999/0966.Vowel Spellchecker/Solution.ts b/solution/0900-0999/0966.Vowel Spellchecker/Solution.ts new file mode 100644 index 0000000000000..dcb347862fb7f --- /dev/null +++ b/solution/0900-0999/0966.Vowel Spellchecker/Solution.ts @@ -0,0 +1,48 @@ +function spellchecker(wordlist: string[], queries: string[]): string[] { + const s = new Set(wordlist); + const low = new Map(); + const pat = new Map(); + + const f = (w: string): string => { + let res = ''; + for (const c of w) { + if ('aeiou'.includes(c)) { + res += '*'; + } else { + res += c; + } + } + return res; + }; + + for (const w of wordlist) { + let t = w.toLowerCase(); + if (!low.has(t)) { + low.set(t, w); + } + t = f(t); + if (!pat.has(t)) { + pat.set(t, w); + } + } + + const ans: string[] = []; + for (let q of queries) { + if (s.has(q)) { + ans.push(q); + continue; + } + q = q.toLowerCase(); + if (low.has(q)) { + ans.push(low.get(q)!); + continue; + } + q = f(q); + if (pat.has(q)) { + ans.push(pat.get(q)!); + continue; + } + ans.push(''); + } + return ans; +} diff --git a/solution/3200-3299/3227.Vowels Game in a String/README.md b/solution/3200-3299/3227.Vowels Game in a String/README.md index 8c089bc98b036..c24d9adb4b350 100644 --- a/solution/3200-3299/3227.Vowels Game in a String/README.md +++ b/solution/3200-3299/3227.Vowels Game in a String/README.md @@ -167,6 +167,38 @@ function doesAliceWin(s: string): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn does_alice_win(s: String) -> bool { + let vowels = "aeiou"; + for c in s.chars() { + if vowels.contains(c) { + return true; + } + } + false + } +} +``` + +#### C# + +```cs +public class Solution { + public bool DoesAliceWin(string s) { + string vowels = "aeiou"; + foreach (char c in s) { + if (vowels.Contains(c)) { + return true; + } + } + return false; + } +} +``` + diff --git a/solution/3200-3299/3227.Vowels Game in a String/README_EN.md b/solution/3200-3299/3227.Vowels Game in a String/README_EN.md index 9c9ce05d5c8ba..bd4a70a295939 100644 --- a/solution/3200-3299/3227.Vowels Game in a String/README_EN.md +++ b/solution/3200-3299/3227.Vowels Game in a String/README_EN.md @@ -165,6 +165,38 @@ function doesAliceWin(s: string): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn does_alice_win(s: String) -> bool { + let vowels = "aeiou"; + for c in s.chars() { + if vowels.contains(c) { + return true; + } + } + false + } +} +``` + +#### C# + +```cs +public class Solution { + public bool DoesAliceWin(string s) { + string vowels = "aeiou"; + foreach (char c in s) { + if (vowels.Contains(c)) { + return true; + } + } + return false; + } +} +``` + diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.cs b/solution/3200-3299/3227.Vowels Game in a String/Solution.cs new file mode 100644 index 0000000000000..bd87e5935f2d9 --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.cs @@ -0,0 +1,11 @@ +public class Solution { + public bool DoesAliceWin(string s) { + string vowels = "aeiou"; + foreach (char c in s) { + if (vowels.Contains(c)) { + return true; + } + } + return false; + } +} diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.rs b/solution/3200-3299/3227.Vowels Game in a String/Solution.rs new file mode 100644 index 0000000000000..a44cfbf3e18b1 --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn does_alice_win(s: String) -> bool { + let vowels = "aeiou"; + for c in s.chars() { + if vowels.contains(c) { + return true; + } + } + false + } +}