diff --git a/lcci/17.13.Re-Space/README.md b/lcci/17.13.Re-Space/README.md index ec551c3356209..cb2dcf9daa0e4 100644 --- a/lcci/17.13.Re-Space/README.md +++ b/lcci/17.13.Re-Space/README.md @@ -108,6 +108,62 @@ func respace(dictionary []string, sentence string) int { } ``` +```swift +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, 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.. diff --git a/lcci/17.13.Re-Space/README_EN.md b/lcci/17.13.Re-Space/README_EN.md index 7e7fe5adb4504..a554fd390cd9f 100644 --- a/lcci/17.13.Re-Space/README_EN.md +++ b/lcci/17.13.Re-Space/README_EN.md @@ -113,6 +113,62 @@ func respace(dictionary []string, sentence string) int { } ``` +```swift +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, 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.. diff --git a/lcci/17.13.Re-Space/Solution.swift b/lcci/17.13.Re-Space/Solution.swift new file mode 100644 index 0000000000000..39c1130229e48 --- /dev/null +++ b/lcci/17.13.Re-Space/Solution.swift @@ -0,0 +1,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, 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..