1408. String Matching in an Array 链接到标题 先按照单词长度进行排序,然后遍历判断当前单词是否被其他单词包含,要注意最终结果应该是去重之后的。
Golang 中可以直接使用 strings.Contains 判断。
type ByLen []string func (a ByLen) Len() int { return len(a) } func (a ByLen) Less(i, j int) bool { return len(a[i]) < len(a[j]) } func (a ByLen) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func stringMatching(words []string) []string { sort.Sort(ByLen(words)) res := []string{} for i := range words { for j := i + 1; j < len(words); j++ { if strings.