From 6db9637fb54c8b3535be8b7af666baac1a9dd176 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 24 May 2025 15:01:18 +0800 Subject: [PATCH] Add solution and test-cases for problem 2942 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 17 ++++++++-- .../Solution_test.go | 21 ++++++------ 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/leetcode/2901-3000/2942.Find-Words-Containing-Character/README.md b/leetcode/2901-3000/2942.Find-Words-Containing-Character/README.md index 5c7d45dd4..b88fee740 100755 --- a/leetcode/2901-3000/2942.Find-Words-Containing-Character/README.md +++ b/leetcode/2901-3000/2942.Find-Words-Containing-Character/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution.go b/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution.go index d115ccf5e..20924c8fb 100644 --- a/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution.go +++ b/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution.go @@ -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 } diff --git a/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution_test.go b/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution_test.go index 14ff50eb4..b4894f6ec 100644 --- a/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution_test.go +++ b/leetcode/2901-3000/2942.Find-Words-Containing-Character/Solution_test.go @@ -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() { }