Skip to content

Commit 6db9637

Browse files
committed
Add solution and test-cases for problem 2942
1 parent a01e89c commit 6db9637

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

leetcode/2901-3000/2942.Find-Words-Containing-Character/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2942.Find Words Containing Character][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** array of strings `words` and a character `x`.
5+
6+
Return an **array of indices** representing the words that contain the character `x`.
7+
8+
**Note** that the returned array may be in **any** order.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: words = ["leet","code"], x = "e"
14+
Output: [0,1]
15+
Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Find Words Containing Character
23-
```go
2420
```
21+
Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
22+
Output: [0,2]
23+
Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.
24+
```
25+
26+
**Example 3:**
2527

28+
```
29+
Input: words = ["abc","bcd","aaaa","cbc"], x = "z"
30+
Output: []
31+
Explanation: "z" does not occur in any of the words. Hence, we return an empty array.
32+
```
2633

2734
## 结语
2835

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(words []string, x byte) []int {
4+
var ans []int
5+
for i, word := range words {
6+
ok := false
7+
for _, b := range []byte(word) {
8+
if b == x {
9+
ok = true
10+
break
11+
}
12+
}
13+
if ok {
14+
ans = append(ans, i)
15+
}
16+
}
17+
return ans
518
}

leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
x byte
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"leet", "code"}, 'e', []int{0, 1}},
18+
{"TestCase2", []string{"abc", "bcd", "aaaa", "cbc"}, 'a', []int{0, 2}},
19+
{"TestCase3", []string{"abc", "bcd", "aaaa", "cbc"}, 'z', nil},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.x)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.x)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)