Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add swift implementation to lcci problem: No.17.15 #2786

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions lcci/17.15.Longest Word/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,66 @@ func longestWord(words []string) string {
}
```

```swift
class Trie {
var children = [Trie?](repeating: nil, count: 26)
var isEnd = false

func insert(_ word: String) {
var node = self
for ch in word {
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[index] == nil {
node.children[index] = Trie()
}
node = node.children[index]!
}
node.isEnd = true
}

func search(_ word: String) -> Bool {
var node = self
for ch in word {
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[index] == nil {
return false
}
node = node.children[index]!
}
return node.isEnd
}
}

class Solution {
func longestWord(_ words: [String]) -> String {
var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
let trie = Trie()

var dfs: ((String) -> Bool)!
dfs = { w in
if w.isEmpty {
return true
}
for i in 1...w.count {
if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
return true
}
}
return false
}

var ans = ""
for w in words {
if dfs(w) {
ans = w
}
trie.insert(w)
}
return ans
}
}
```

<!-- tabs:end -->

<!-- end -->
60 changes: 60 additions & 0 deletions lcci/17.15.Longest Word/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,66 @@ func longestWord(words []string) string {
}
```

```swift
class Trie {
var children = [Trie?](repeating: nil, count: 26)
var isEnd = false

func insert(_ word: String) {
var node = self
for ch in word {
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[index] == nil {
node.children[index] = Trie()
}
node = node.children[index]!
}
node.isEnd = true
}

func search(_ word: String) -> Bool {
var node = self
for ch in word {
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[index] == nil {
return false
}
node = node.children[index]!
}
return node.isEnd
}
}

class Solution {
func longestWord(_ words: [String]) -> String {
var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
let trie = Trie()

var dfs: ((String) -> Bool)!
dfs = { w in
if w.isEmpty {
return true
}
for i in 1...w.count {
if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
return true
}
}
return false
}

var ans = ""
for w in words {
if dfs(w) {
ans = w
}
trie.insert(w)
}
return ans
}
}
```

<!-- tabs:end -->

<!-- end -->
57 changes: 57 additions & 0 deletions lcci/17.15.Longest Word/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Trie {
var children = [Trie?](repeating: nil, count: 26)
var isEnd = false

func insert(_ word: String) {
var node = self
for ch in word {
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[index] == nil {
node.children[index] = Trie()
}
node = node.children[index]!
}
node.isEnd = true
}

func search(_ word: String) -> Bool {
var node = self
for ch in word {
let index = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[index] == nil {
return false
}
node = node.children[index]!
}
return node.isEnd
}
}

class Solution {
func longestWord(_ words: [String]) -> String {
var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
let trie = Trie()

var dfs: ((String) -> Bool)!
dfs = { w in
if w.isEmpty {
return true
}
for i in 1...w.count {
if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
return true
}
}
return false
}

var ans = ""
for w in words {
if dfs(w) {
ans = w
}
trie.insert(w)
}
return ans
}
}