Skip to content

Commit 0378ee4

Browse files
authored
feat: add swift implementation to lcci problem: No.17.13 (doocs#2784)
1 parent 7321914 commit 0378ee4

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

lcci/17.13.Re-Space/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,62 @@ func respace(dictionary []string, sentence string) int {
108108
}
109109
```
110110

111+
```swift
112+
class TrieNode {
113+
var children: [TrieNode?] = Array(repeating: nil, count: 26)
114+
var isEndOfWord = false
115+
}
116+
117+
class Trie {
118+
private let root = TrieNode()
119+
120+
func insert(_ word: String) {
121+
var node = root
122+
for char in word {
123+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
124+
if node.children[index] == nil {
125+
node.children[index] = TrieNode()
126+
}
127+
node = node.children[index]!
128+
}
129+
node.isEndOfWord = true
130+
}
131+
132+
func search(_ sentence: Array<Character>, start: Int, end: Int) -> Bool {
133+
var node = root
134+
for i in start...end {
135+
let index = Int(sentence[i].asciiValue! - Character("a").asciiValue!)
136+
guard let nextNode = node.children[index] else {
137+
return false
138+
}
139+
node = nextNode
140+
}
141+
return node.isEndOfWord
142+
}
143+
}
144+
145+
class Solution {
146+
func respace(_ dictionary: [String], _ sentence: String) -> Int {
147+
let n = sentence.count
148+
guard n > 0 else { return 0 }
149+
let trie = Trie()
150+
dictionary.forEach { trie.insert($0) }
151+
let chars = Array(sentence)
152+
var dp = Array(repeating: Int.max, count: n + 1)
153+
dp[0] = 0
154+
for i in 1...n {
155+
dp[i] = dp[i - 1] + 1
156+
for j in 0..<i {
157+
if trie.search(chars, start: j, end: i - 1) {
158+
dp[i] = min(dp[i], dp[j])
159+
}
160+
}
161+
}
162+
return dp[n]
163+
}
164+
}
165+
```
166+
111167
<!-- tabs:end -->
112168

113169
<!-- end -->

lcci/17.13.Re-Space/README_EN.md

+56
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,62 @@ func respace(dictionary []string, sentence string) int {
113113
}
114114
```
115115

116+
```swift
117+
class TrieNode {
118+
var children: [TrieNode?] = Array(repeating: nil, count: 26)
119+
var isEndOfWord = false
120+
}
121+
122+
class Trie {
123+
private let root = TrieNode()
124+
125+
func insert(_ word: String) {
126+
var node = root
127+
for char in word {
128+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
129+
if node.children[index] == nil {
130+
node.children[index] = TrieNode()
131+
}
132+
node = node.children[index]!
133+
}
134+
node.isEndOfWord = true
135+
}
136+
137+
func search(_ sentence: Array<Character>, start: Int, end: Int) -> Bool {
138+
var node = root
139+
for i in start...end {
140+
let index = Int(sentence[i].asciiValue! - Character("a").asciiValue!)
141+
guard let nextNode = node.children[index] else {
142+
return false
143+
}
144+
node = nextNode
145+
}
146+
return node.isEndOfWord
147+
}
148+
}
149+
150+
class Solution {
151+
func respace(_ dictionary: [String], _ sentence: String) -> Int {
152+
let n = sentence.count
153+
guard n > 0 else { return 0 }
154+
let trie = Trie()
155+
dictionary.forEach { trie.insert($0) }
156+
let chars = Array(sentence)
157+
var dp = Array(repeating: Int.max, count: n + 1)
158+
dp[0] = 0
159+
for i in 1...n {
160+
dp[i] = dp[i - 1] + 1
161+
for j in 0..<i {
162+
if trie.search(chars, start: j, end: i - 1) {
163+
dp[i] = min(dp[i], dp[j])
164+
}
165+
}
166+
}
167+
return dp[n]
168+
}
169+
}
170+
```
171+
116172
<!-- tabs:end -->
117173

118174
<!-- end -->

lcci/17.13.Re-Space/Solution.swift

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class TrieNode {
2+
var children: [TrieNode?] = Array(repeating: nil, count: 26)
3+
var isEndOfWord = false
4+
}
5+
6+
class Trie {
7+
private let root = TrieNode()
8+
9+
func insert(_ word: String) {
10+
var node = root
11+
for char in word {
12+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
13+
if node.children[index] == nil {
14+
node.children[index] = TrieNode()
15+
}
16+
node = node.children[index]!
17+
}
18+
node.isEndOfWord = true
19+
}
20+
21+
func search(_ sentence: Array<Character>, start: Int, end: Int) -> Bool {
22+
var node = root
23+
for i in start...end {
24+
let index = Int(sentence[i].asciiValue! - Character("a").asciiValue!)
25+
guard let nextNode = node.children[index] else {
26+
return false
27+
}
28+
node = nextNode
29+
}
30+
return node.isEndOfWord
31+
}
32+
}
33+
34+
class Solution {
35+
func respace(_ dictionary: [String], _ sentence: String) -> Int {
36+
let n = sentence.count
37+
guard n > 0 else { return 0 }
38+
let trie = Trie()
39+
dictionary.forEach { trie.insert($0) }
40+
let chars = Array(sentence)
41+
var dp = Array(repeating: Int.max, count: n + 1)
42+
dp[0] = 0
43+
for i in 1...n {
44+
dp[i] = dp[i - 1] + 1
45+
for j in 0..<i {
46+
if trie.search(chars, start: j, end: i - 1) {
47+
dp[i] = min(dp[i], dp[j])
48+
}
49+
}
50+
}
51+
return dp[n]
52+
}
53+
}

0 commit comments

Comments
 (0)