Skip to content

Commit 7321914

Browse files
klever34yanglbme
andauthored
feat: add swift implementation to lcci problem: No.17.15 (doocs#2786)
--------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 589542d commit 7321914

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

lcci/17.15.Longest Word/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,66 @@ func longestWord(words []string) string {
199199
}
200200
```
201201

202+
```swift
203+
class Trie {
204+
var children = [Trie?](repeating: nil, count: 26)
205+
var isEnd = false
206+
207+
func insert(_ word: String) {
208+
var node = self
209+
for ch in word {
210+
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
211+
if node.children[index] == nil {
212+
node.children[index] = Trie()
213+
}
214+
node = node.children[index]!
215+
}
216+
node.isEnd = true
217+
}
218+
219+
func search(_ word: String) -> Bool {
220+
var node = self
221+
for ch in word {
222+
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
223+
if node.children[index] == nil {
224+
return false
225+
}
226+
node = node.children[index]!
227+
}
228+
return node.isEnd
229+
}
230+
}
231+
232+
class Solution {
233+
func longestWord(_ words: [String]) -> String {
234+
var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
235+
let trie = Trie()
236+
237+
var dfs: ((String) -> Bool)!
238+
dfs = { w in
239+
if w.isEmpty {
240+
return true
241+
}
242+
for i in 1...w.count {
243+
if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
244+
return true
245+
}
246+
}
247+
return false
248+
}
249+
250+
var ans = ""
251+
for w in words {
252+
if dfs(w) {
253+
ans = w
254+
}
255+
trie.insert(w)
256+
}
257+
return ans
258+
}
259+
}
260+
```
261+
202262
<!-- tabs:end -->
203263

204264
<!-- end -->

lcci/17.15.Longest Word/README_EN.md

+60
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,66 @@ func longestWord(words []string) string {
207207
}
208208
```
209209

210+
```swift
211+
class Trie {
212+
var children = [Trie?](repeating: nil, count: 26)
213+
var isEnd = false
214+
215+
func insert(_ word: String) {
216+
var node = self
217+
for ch in word {
218+
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
219+
if node.children[index] == nil {
220+
node.children[index] = Trie()
221+
}
222+
node = node.children[index]!
223+
}
224+
node.isEnd = true
225+
}
226+
227+
func search(_ word: String) -> Bool {
228+
var node = self
229+
for ch in word {
230+
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
231+
if node.children[index] == nil {
232+
return false
233+
}
234+
node = node.children[index]!
235+
}
236+
return node.isEnd
237+
}
238+
}
239+
240+
class Solution {
241+
func longestWord(_ words: [String]) -> String {
242+
var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
243+
let trie = Trie()
244+
245+
var dfs: ((String) -> Bool)!
246+
dfs = { w in
247+
if w.isEmpty {
248+
return true
249+
}
250+
for i in 1...w.count {
251+
if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
252+
return true
253+
}
254+
}
255+
return false
256+
}
257+
258+
var ans = ""
259+
for w in words {
260+
if dfs(w) {
261+
ans = w
262+
}
263+
trie.insert(w)
264+
}
265+
return ans
266+
}
267+
}
268+
```
269+
210270
<!-- tabs:end -->
211271

212272
<!-- end -->
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Trie {
2+
var children = [Trie?](repeating: nil, count: 26)
3+
var isEnd = false
4+
5+
func insert(_ word: String) {
6+
var node = self
7+
for ch in word {
8+
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
9+
if node.children[index] == nil {
10+
node.children[index] = Trie()
11+
}
12+
node = node.children[index]!
13+
}
14+
node.isEnd = true
15+
}
16+
17+
func search(_ word: String) -> Bool {
18+
var node = self
19+
for ch in word {
20+
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
21+
if node.children[index] == nil {
22+
return false
23+
}
24+
node = node.children[index]!
25+
}
26+
return node.isEnd
27+
}
28+
}
29+
30+
class Solution {
31+
func longestWord(_ words: [String]) -> String {
32+
var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
33+
let trie = Trie()
34+
35+
var dfs: ((String) -> Bool)!
36+
dfs = { w in
37+
if w.isEmpty {
38+
return true
39+
}
40+
for i in 1...w.count {
41+
if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
42+
return true
43+
}
44+
}
45+
return false
46+
}
47+
48+
var ans = ""
49+
for w in words {
50+
if dfs(w) {
51+
ans = w
52+
}
53+
trie.insert(w)
54+
}
55+
return ans
56+
}
57+
}

0 commit comments

Comments
 (0)