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