Skip to content

Commit 6085f2f

Browse files
authored
feat: add solutions to lc problem: No.2942 (#4424)
No.2942.Find Words Containing Character
1 parent 4217576 commit 6085f2f

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

solution/2900-2999/2942.Find Words Containing Character/README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,19 @@ func findWordsContaining(words []string, x byte) (ans []int) {
139139

140140
```ts
141141
function findWordsContaining(words: string[], x: string): number[] {
142-
const ans: number[] = [];
143-
for (let i = 0; i < words.length; ++i) {
144-
if (words[i].includes(x)) {
145-
ans.push(i);
146-
}
142+
return words.flatMap((w, i) => (w.includes(x) ? [i] : []));
143+
}
144+
```
145+
146+
#### Rust
147+
148+
```rust
149+
impl Solution {
150+
pub fn find_words_containing(words: Vec<String>, x: char) -> Vec<i32> {
151+
words.into_iter().enumerate()
152+
.filter_map(|(i, w)| w.contains(x).then(|| i as i32))
153+
.collect()
147154
}
148-
return ans;
149155
}
150156
```
151157

solution/2900-2999/2942.Find Words Containing Character/README_EN.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,19 @@ func findWordsContaining(words []string, x byte) (ans []int) {
137137

138138
```ts
139139
function findWordsContaining(words: string[], x: string): number[] {
140-
const ans: number[] = [];
141-
for (let i = 0; i < words.length; ++i) {
142-
if (words[i].includes(x)) {
143-
ans.push(i);
144-
}
140+
return words.flatMap((w, i) => (w.includes(x) ? [i] : []));
141+
}
142+
```
143+
144+
#### Rust
145+
146+
```rust
147+
impl Solution {
148+
pub fn find_words_containing(words: Vec<String>, x: char) -> Vec<i32> {
149+
words.into_iter().enumerate()
150+
.filter_map(|(i, w)| w.contains(x).then(|| i as i32))
151+
.collect()
145152
}
146-
return ans;
147153
}
148154
```
149155

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn find_words_containing(words: Vec<String>, x: char) -> Vec<i32> {
3+
words
4+
.into_iter()
5+
.enumerate()
6+
.filter_map(|(i, w)| w.contains(x).then(|| i as i32))
7+
.collect()
8+
}
9+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
function findWordsContaining(words: string[], x: string): number[] {
2-
const ans: number[] = [];
3-
for (let i = 0; i < words.length; ++i) {
4-
if (words[i].includes(x)) {
5-
ans.push(i);
6-
}
7-
}
8-
return ans;
2+
return words.flatMap((w, i) => (w.includes(x) ? [i] : []));
93
}

0 commit comments

Comments
 (0)