Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions leetcode/2901-3000/2942.Find-Words-Containing-Character/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [2942.Find Words Containing Character][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a **0-indexed** array of strings `words` and a character `x`.

Return an **array of indices** representing the words that contain the character `x`.

**Note** that the returned array may be in **any** order.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: words = ["leet","code"], x = "e"
Output: [0,1]
Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Find Words Containing Character
```go
```
Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
Output: [0,2]
Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.
```

**Example 3:**

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

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(words []string, x byte) []int {
var ans []int
for i, word := range words {
ok := false
for _, b := range []byte(word) {
if b == x {
ok = true
break
}
}
if ok {
ans = append(ans, i)
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
x byte
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"leet", "code"}, 'e', []int{0, 1}},
{"TestCase2", []string{"abc", "bcd", "aaaa", "cbc"}, 'a', []int{0, 2}},
{"TestCase3", []string{"abc", "bcd", "aaaa", "cbc"}, 'z', nil},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.x)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.x)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading