Skip to content

Commit 601c7bd

Browse files
committed
Swift Implementation for LCCI 17.15
1 parent a88a827 commit 601c7bd

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

lcci/17.15.Longest Word/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,73 @@ 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+
private let trie = Trie()
234+
235+
func longestWord(_ words: [String]) -> String {
236+
let sortedWords = words.sorted { a, b in
237+
if a.count != b.count {
238+
return a.count < b.count
239+
}
240+
return a > b
241+
}
242+
243+
var ans = ""
244+
for word in sortedWords {
245+
if dfs(word, "") {
246+
ans = word
247+
}
248+
trie.insert(word)
249+
}
250+
return ans
251+
}
252+
253+
private func dfs(_ word: String, _ prefix: String) -> Bool {
254+
if prefix.isEmpty {
255+
return true
256+
}
257+
for i in 1...prefix.count {
258+
let index = prefix.index(prefix.startIndex, offsetBy: i)
259+
let subPrefix = String(prefix[..<index])
260+
if trie.search(subPrefix) && dfs(word, String(prefix[index...])) {
261+
return true
262+
}
263+
}
264+
return false
265+
}
266+
}
267+
```
268+
202269
<!-- tabs:end -->
203270

204271
<!-- end -->

lcci/17.15.Longest Word/README_EN.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,73 @@ 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+
private let trie = Trie()
242+
243+
func longestWord(_ words: [String]) -> String {
244+
let sortedWords = words.sorted { a, b in
245+
if a.count != b.count {
246+
return a.count < b.count
247+
}
248+
return a > b
249+
}
250+
251+
var ans = ""
252+
for word in sortedWords {
253+
if dfs(word, "") {
254+
ans = word
255+
}
256+
trie.insert(word)
257+
}
258+
return ans
259+
}
260+
261+
private func dfs(_ word: String, _ prefix: String) -> Bool {
262+
if prefix.isEmpty {
263+
return true
264+
}
265+
for i in 1...prefix.count {
266+
let index = prefix.index(prefix.startIndex, offsetBy: i)
267+
let subPrefix = String(prefix[..<index])
268+
if trie.search(subPrefix) && dfs(word, String(prefix[index...])) {
269+
return true
270+
}
271+
}
272+
return false
273+
}
274+
}
275+
```
276+
210277
<!-- tabs:end -->
211278

212279
<!-- end -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
private let trie = Trie()
32+
33+
func longestWord(_ words: [String]) -> String {
34+
let sortedWords = words.sorted { a, b in
35+
if a.count != b.count {
36+
return a.count < b.count
37+
}
38+
return a > b
39+
}
40+
41+
var ans = ""
42+
for word in sortedWords {
43+
if dfs(word, "") {
44+
ans = word
45+
}
46+
trie.insert(word)
47+
}
48+
return ans
49+
}
50+
51+
private func dfs(_ word: String, _ prefix: String) -> Bool {
52+
if prefix.isEmpty {
53+
return true
54+
}
55+
for i in 1...prefix.count {
56+
let index = prefix.index(prefix.startIndex, offsetBy: i)
57+
let subPrefix = String(prefix[..<index])
58+
if trie.search(subPrefix) && dfs(word, String(prefix[index...])) {
59+
return true
60+
}
61+
}
62+
return false
63+
}
64+
}

0 commit comments

Comments
 (0)