From 91d3403040f2f52f4980654aee46df5d608511da Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 23 May 2025 12:38:26 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2942 No.2942.Find Words Containing Character --- .../README.md | 18 ++++++++++++------ .../README_EN.md | 18 ++++++++++++------ .../Solution.rs | 9 +++++++++ .../Solution.ts | 8 +------- 4 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 solution/2900-2999/2942.Find Words Containing Character/Solution.rs diff --git a/solution/2900-2999/2942.Find Words Containing Character/README.md b/solution/2900-2999/2942.Find Words Containing Character/README.md index ac978867e8681..8f18d567e267f 100644 --- a/solution/2900-2999/2942.Find Words Containing Character/README.md +++ b/solution/2900-2999/2942.Find Words Containing Character/README.md @@ -139,13 +139,19 @@ func findWordsContaining(words []string, x byte) (ans []int) { ```ts function findWordsContaining(words: string[], x: string): number[] { - const ans: number[] = []; - for (let i = 0; i < words.length; ++i) { - if (words[i].includes(x)) { - ans.push(i); - } + return words.flatMap((w, i) => (w.includes(x) ? [i] : [])); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn find_words_containing(words: Vec, x: char) -> Vec { + words.into_iter().enumerate() + .filter_map(|(i, w)| w.contains(x).then(|| i as i32)) + .collect() } - return ans; } ``` diff --git a/solution/2900-2999/2942.Find Words Containing Character/README_EN.md b/solution/2900-2999/2942.Find Words Containing Character/README_EN.md index 26bc8faf7efcb..3b8c94867fd85 100644 --- a/solution/2900-2999/2942.Find Words Containing Character/README_EN.md +++ b/solution/2900-2999/2942.Find Words Containing Character/README_EN.md @@ -137,13 +137,19 @@ func findWordsContaining(words []string, x byte) (ans []int) { ```ts function findWordsContaining(words: string[], x: string): number[] { - const ans: number[] = []; - for (let i = 0; i < words.length; ++i) { - if (words[i].includes(x)) { - ans.push(i); - } + return words.flatMap((w, i) => (w.includes(x) ? [i] : [])); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn find_words_containing(words: Vec, x: char) -> Vec { + words.into_iter().enumerate() + .filter_map(|(i, w)| w.contains(x).then(|| i as i32)) + .collect() } - return ans; } ``` diff --git a/solution/2900-2999/2942.Find Words Containing Character/Solution.rs b/solution/2900-2999/2942.Find Words Containing Character/Solution.rs new file mode 100644 index 0000000000000..0fbec1bebab82 --- /dev/null +++ b/solution/2900-2999/2942.Find Words Containing Character/Solution.rs @@ -0,0 +1,9 @@ +impl Solution { + pub fn find_words_containing(words: Vec, x: char) -> Vec { + words + .into_iter() + .enumerate() + .filter_map(|(i, w)| w.contains(x).then(|| i as i32)) + .collect() + } +} diff --git a/solution/2900-2999/2942.Find Words Containing Character/Solution.ts b/solution/2900-2999/2942.Find Words Containing Character/Solution.ts index 63c0b4f674f10..2a6e7a95d9160 100644 --- a/solution/2900-2999/2942.Find Words Containing Character/Solution.ts +++ b/solution/2900-2999/2942.Find Words Containing Character/Solution.ts @@ -1,9 +1,3 @@ function findWordsContaining(words: string[], x: string): number[] { - const ans: number[] = []; - for (let i = 0; i < words.length; ++i) { - if (words[i].includes(x)) { - ans.push(i); - } - } - return ans; + return words.flatMap((w, i) => (w.includes(x) ? [i] : [])); }