-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
41 lines (29 loc) · 1.18 KB
/
Contents.swift
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
/*:
# 数组中的字符串匹配
- 题号:[5380](https://leetcode-cn.com/problems/string-matching-in-an-array/)
- 难度:简单
- 描述:
给你一个字符串数组 `words` ,数组中的每个字符串都可以看作是一个单词。
请你按 **任意** 顺序返回 `words` 中是其他单词的子字符串的所有单词。
如果你可以删除 `words[j]` 最左侧和/或最右侧的若干字符得到 `word[i]` ,
那么字符串 `words[i]` 就是 `words[j]` 的一个子字符串。
*/
//: ## Code
import Foundation
func stringMatching(_ words: [String]) -> [String] {
var result = Set<String>()
for word in words {
for tmp in words {
if tmp != word && tmp.contains(word) {
result.insert(word)
}
}
}
return [String](result)
}
//: ## Test
print(stringMatching(["leetcoder","leetcode","od","hamlet","am"])) // ["leetcode","od","am"]
print(stringMatching(["mass","as","hero","superhero"])) // ["as","hero"]
print(stringMatching(["leetcode","et","code"])) // ["et","code"]
print(stringMatching(["blue","green","bu"])) // []
//: [下一道题](@next)