Skip to content

Commit 2bea406

Browse files
authored
Merge pull request #1086 from 0xff-dev/1408
Add solution and test-cases for problem 1408
2 parents 5ab0440 + f092489 commit 2bea406

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [1408.String Matching in an Array][title]
2+
3+
## Description
4+
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**.
5+
6+
A **substring** is a contiguous sequence of characters within a strings
7+
8+
**Example 1:**
9+
10+
```
11+
Input: words = ["mass","as","hero","superhero"]
12+
Output: ["as","hero"]
13+
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
14+
["hero","as"] is also a valid answer.
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: words = ["leetcode","et","code"]
21+
Output: ["et","code"]
22+
Explanation: "et", "code" are substring of "leetcode".
23+
```
24+
25+
**Example 3:**
26+
27+
```
28+
Input: words = ["blue","green","bu"]
29+
Output: []
30+
Explanation: No string of words is substring of another string.
31+
```
32+
33+
## 结语
34+
35+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
36+
37+
[title]: https://leetcode.com/problems/string-matching-in-an-array
38+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(words []string) []string {
6+
ans := make([]string, 0)
7+
for i := range words {
8+
for j := range words {
9+
if j == i {
10+
continue
11+
}
12+
if strings.Contains(words[j], words[i]) {
13+
ans = append(ans, words[i])
14+
break
15+
}
16+
}
17+
}
18+
return ans
519
}

leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"mass", "as", "hero", "superhero"}, []string{"as", "hero"}},
17+
{"TestCase2", []string{"leetcode", "et", "code"}, []string{"et", "code"}},
18+
{"TestCase3", []string{"blue", "green", "bu"}, []string{}},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)