| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent db20f09 commit 2b135fe
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + // | ||
| 2 | + // LeetCode_720_031.swift | ||
| 3 | + // TestCoding | ||
| 4 | + // | ||
| 5 | + // Created by 龚欢 on 2019/5/11. | ||
| 6 | + // Copyright © 2019 龚欢. All rights reserved. | ||
| 7 | + // | ||
| 8 | + | ||
| 9 | + import Foundation | ||
| 10 | + | ||
| 11 | + class Solution { | ||
| 12 | + func longestWord(_ words: [String]) -> String { | ||
| 13 | + let sortedWords = Set(words).sorted { | ||
| 14 | + if $0.count == $1.count { | ||
| 15 | + return $0 < $1 | ||
| 16 | + } else { | ||
| 17 | + return $0.count > $1.count | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + var errorDatas: Set<String> = [] | ||
| 21 | + for key in sortedWords { | ||
| 22 | + if (errorDatas.contains(key)) { continue } | ||
| 23 | + for i in 1...key.count { | ||
| 24 | + let subString = String(key[key.startIndex..<key.index(key.startIndex, offsetBy: i)]) | ||
| 25 | + if (sortedWords.contains(subString)) { | ||
| 26 | + if i == key.count { return key } | ||
| 27 | + continue | ||
| 28 | + } else { | ||
| 29 | + errorDatas.insert(key) | ||
| 30 | + break | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + return "" | ||
| 35 | + } | ||
| 36 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments