Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1408 #1086

Merged
merged 1 commit into from
Jan 9, 2025
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
38 changes: 38 additions & 0 deletions leetcode/1401-1500/1408.String-Matching-in-an-Array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# [1408.String Matching in an Array][title]

## Description
Given an array of string `words`, return all strings in `words` that is a **substring** of another word. You can return the answer in **any order**.

A **substring** is a contiguous sequence of characters within a strings

**Example 1:**

```
Input: words = ["mass","as","hero","superhero"]
Output: ["as","hero"]
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
["hero","as"] is also a valid answer.
```

**Example 2:**

```
Input: words = ["leetcode","et","code"]
Output: ["et","code"]
Explanation: "et", "code" are substring of "leetcode".
```

**Example 3:**

```
Input: words = ["blue","green","bu"]
Output: []
Explanation: No string of words is substring of another string.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/string-matching-in-an-array
[me]: https://github.com/kylesliu/awesome-golang-algorithm
18 changes: 16 additions & 2 deletions leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

func Solution(words []string) []string {
ans := make([]string, 0)
for i := range words {
for j := range words {
if j == i {
continue
}
if strings.Contains(words[j], words[i]) {
ans = append(ans, words[i])
break
}
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"mass", "as", "hero", "superhero"}, []string{"as", "hero"}},
{"TestCase2", []string{"leetcode", "et", "code"}, []string{"et", "code"}},
{"TestCase3", []string{"blue", "green", "bu"}, []string{}},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

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

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