FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add leetcode 720 · feixiangcode/algorithm@2b135fe · GitHub

Commit 2b135fe

Browse files
CRGT_Ghcoder
committed
add leetcode 720
1 parent db20f09 commit 2b135fe

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL