diff --git a/leetcode/1401-1500/1408.String-Matching-in-an-Array/README.md b/leetcode/1401-1500/1408.String-Matching-in-an-Array/README.md new file mode 100644 index 000000000..35a414933 --- /dev/null +++ b/leetcode/1401-1500/1408.String-Matching-in-an-Array/README.md @@ -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 diff --git a/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution.go b/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution.go index d115ccf5e..6ff3edd15 100755 --- a/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution.go +++ b/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution.go @@ -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 } diff --git a/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution_test.go b/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution_test.go index 14ff50eb4..cc33dbe30 100755 --- a/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution_test.go +++ b/leetcode/1401-1500/1408.String-Matching-in-an-Array/Solution_test.go @@ -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{}}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }