-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1408-StringMatchingInAnArray.go
More file actions
105 lines (94 loc) · 3.39 KB
/
1408-StringMatchingInAnArray.go
File metadata and controls
105 lines (94 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
// 1408. String Matching in an Array
// 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 string
// 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.
// Constraints:
// 1 <= words.length <= 100
// 1 <= words[i].length <= 30
// words[i] contains only lowercase English letters.
// All the strings of words are unique.
import "fmt"
import "sort"
import "strings"
func stringMatching(words []string) []string {
mp, res := make(map[string]bool), []string{}
for _, v1 := range words {
for _, v2 := range words {
if v1 == v2 || len(v2) > len(v1) { continue } // 字符串一样或者长度更多就没有了必要判断了
if _, ok := mp[v2]; !ok && strings.Contains(v1, v2) {
mp[v2] = true
res = append(res, v2)
}
}
}
return res
}
func stringMatching1(words []string) []string {
sort.Slice(words, func(i, j int) bool { // 从小到大排序
return len(words[i]) < len(words[j])
})
isSubStr := func(s, t string) bool {
if len(s) < len(t) { return false }
for i := 0; i < len(s); i++ {
if s[i] == t[0] {
p := 1
for k := i + 1; p < len(t) && k < len(s); {
if s[k] != t[p] { break }
k++
p++
}
if p == len(t) {
return true
}
}
}
return false
}
res, mp := []string{}, map[string]struct{}{}
for i := 1; i < len(words); i++ {
for j := i - 1; j >= 0; j-- {
if isSubStr(words[i], words[j]) {
mp[words[j]] = struct{}{}
}
}
}
for k := range mp {
res = append(res, k)
}
return res
}
func main() {
// 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.
fmt.Println(stringMatching([]string{"mass","as","hero","superhero"})) // ["as","hero"]
// Example 2:
// Input: words = ["leetcode","et","code"]
// Output: ["et","code"]
// Explanation: "et", "code" are substring of "leetcode".
fmt.Println(stringMatching([]string{"leetcode","et","code"})) // ["et","code"]
// Example 3:
// Input: words = ["blue","green","bu"]
// Output: []
// Explanation: No string of words is substring of another string.
fmt.Println(stringMatching([]string{"blue","green","bu"})) // []
fmt.Println(stringMatching1([]string{"mass","as","hero","superhero"})) // ["as","hero"]
fmt.Println(stringMatching1([]string{"leetcode","et","code"})) // ["et","code"]
fmt.Println(stringMatching1([]string{"blue","green","bu"})) // []
}