From f84aa7712fada7531be4f4d9300bd7c361824ab9 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 16 May 2024 07:41:30 +0100 Subject: [PATCH 1/5] Swift Implementation for LCCI 17.25 --- lcci/17.25.Word Rectangle/README.md | 81 ++++++++++++++++++++++++ lcci/17.25.Word Rectangle/README_EN.md | 81 ++++++++++++++++++++++++ lcci/17.25.Word Rectangle/Solution.swift | 78 +++++++++++++++++++++++ 3 files changed, 240 insertions(+) create mode 100644 lcci/17.25.Word Rectangle/Solution.swift diff --git a/lcci/17.25.Word Rectangle/README.md b/lcci/17.25.Word Rectangle/README.md index 4786b627e7815..70e917dabcc31 100644 --- a/lcci/17.25.Word Rectangle/README.md +++ b/lcci/17.25.Word Rectangle/README.md @@ -349,6 +349,87 @@ func maxRectangle(words []string) (ans []string) { } ``` +```swift +class Trie { + var children = [Trie?](repeating: nil, count: 26) + var isEnd = false + + func insert(_ word: String) { + var node = self + for c in word { + let index = Int(c.asciiValue! - Character("a").asciiValue!) + if node.children[index] == nil { + node.children[index] = Trie() + } + node = node.children[index]! + } + node.isEnd = true + } +} + +class Solution { + private var maxL = 0 + private var maxS = 0 + private var ans: [String]? + private var trie = Trie() + private var t = [String]() + + func maxRectangle(_ words: [String]) -> [String]? { + var d = [Int: [String]]() + for word in words { + maxL = max(maxL, word.count) + trie.insert(word) + d[word.count, default: []].append(word) + } + + for ws in d.values { + t.removeAll() + dfs(ws) + } + return ans + } + + private func dfs(_ ws: [String]) { + guard let first = ws.first, first.count * maxL > maxS, t.count < maxL else { return } + for w in ws { + t.append(w) + let st = check(t) + switch st { + case 0: + t.removeLast() + case 1: + if maxS < t.count * t[0].count { + maxS = t.count * t[0].count + ans = t + } + dfs(ws) + t.removeLast() + default: + dfs(ws) + t.removeLast() + } + } + } + + private func check(_ mat: [String]) -> Int { + let m = mat.count, n = mat[0].count + var result = 1 + for j in 0.. diff --git a/lcci/17.25.Word Rectangle/README_EN.md b/lcci/17.25.Word Rectangle/README_EN.md index abac923c3dffd..265bd9717ece9 100644 --- a/lcci/17.25.Word Rectangle/README_EN.md +++ b/lcci/17.25.Word Rectangle/README_EN.md @@ -354,6 +354,87 @@ func maxRectangle(words []string) (ans []string) { } ``` +```swift +class Trie { + var children = [Trie?](repeating: nil, count: 26) + var isEnd = false + + func insert(_ word: String) { + var node = self + for c in word { + let index = Int(c.asciiValue! - Character("a").asciiValue!) + if node.children[index] == nil { + node.children[index] = Trie() + } + node = node.children[index]! + } + node.isEnd = true + } +} + +class Solution { + private var maxL = 0 + private var maxS = 0 + private var ans: [String]? + private var trie = Trie() + private var t = [String]() + + func maxRectangle(_ words: [String]) -> [String]? { + var d = [Int: [String]]() + for word in words { + maxL = max(maxL, word.count) + trie.insert(word) + d[word.count, default: []].append(word) + } + + for ws in d.values { + t.removeAll() + dfs(ws) + } + return ans + } + + private func dfs(_ ws: [String]) { + guard let first = ws.first, first.count * maxL > maxS, t.count < maxL else { return } + for w in ws { + t.append(w) + let st = check(t) + switch st { + case 0: + t.removeLast() + case 1: + if maxS < t.count * t[0].count { + maxS = t.count * t[0].count + ans = t + } + dfs(ws) + t.removeLast() + default: + dfs(ws) + t.removeLast() + } + } + } + + private func check(_ mat: [String]) -> Int { + let m = mat.count, n = mat[0].count + var result = 1 + for j in 0.. diff --git a/lcci/17.25.Word Rectangle/Solution.swift b/lcci/17.25.Word Rectangle/Solution.swift new file mode 100644 index 0000000000000..4973031569dfa --- /dev/null +++ b/lcci/17.25.Word Rectangle/Solution.swift @@ -0,0 +1,78 @@ +class Trie { + var children = [Trie?](repeating: nil, count: 26) + var isEnd = false + + func insert(_ word: String) { + var node = self + for c in word { + let index = Int(c.asciiValue! - Character("a").asciiValue!) + if node.children[index] == nil { + node.children[index] = Trie() + } + node = node.children[index]! + } + node.isEnd = true + } +} + +class Solution { + private var maxL = 0 + private var maxS = 0 + private var ans: [String]? + private var trie = Trie() + private var t = [String]() + + func maxRectangle(_ words: [String]) -> [String]? { + var d = [Int: [String]]() + for word in words { + maxL = max(maxL, word.count) + trie.insert(word) + d[word.count, default: []].append(word) + } + + for ws in d.values { + t.removeAll() + dfs(ws) + } + return ans + } + + private func dfs(_ ws: [String]) { + guard let first = ws.first, first.count * maxL > maxS, t.count < maxL else { return } + for w in ws { + t.append(w) + let st = check(t) + switch st { + case 0: + t.removeLast() + case 1: + if maxS < t.count * t[0].count { + maxS = t.count * t[0].count + ans = t + } + dfs(ws) + t.removeLast() + default: + dfs(ws) + t.removeLast() + } + } + } + + private func check(_ mat: [String]) -> Int { + let m = mat.count, n = mat[0].count + var result = 1 + for j in 0.. Date: Thu, 16 May 2024 07:03:07 +0000 Subject: [PATCH 2/5] style: format code and docs with prettier --- lcci/17.25.Word Rectangle/README.md | 4 ++-- lcci/17.25.Word Rectangle/README_EN.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lcci/17.25.Word Rectangle/README.md b/lcci/17.25.Word Rectangle/README.md index 70e917dabcc31..8a7aeb1bc7b0e 100644 --- a/lcci/17.25.Word Rectangle/README.md +++ b/lcci/17.25.Word Rectangle/README.md @@ -353,7 +353,7 @@ func maxRectangle(words []string) (ans []string) { class Trie { var children = [Trie?](repeating: nil, count: 26) var isEnd = false - + func insert(_ word: String) { var node = self for c in word { @@ -381,7 +381,7 @@ class Solution { trie.insert(word) d[word.count, default: []].append(word) } - + for ws in d.values { t.removeAll() dfs(ws) diff --git a/lcci/17.25.Word Rectangle/README_EN.md b/lcci/17.25.Word Rectangle/README_EN.md index 265bd9717ece9..8bc84f25a39a3 100644 --- a/lcci/17.25.Word Rectangle/README_EN.md +++ b/lcci/17.25.Word Rectangle/README_EN.md @@ -358,7 +358,7 @@ func maxRectangle(words []string) (ans []string) { class Trie { var children = [Trie?](repeating: nil, count: 26) var isEnd = false - + func insert(_ word: String) { var node = self for c in word { @@ -386,7 +386,7 @@ class Solution { trie.insert(word) d[word.count, default: []].append(word) } - + for ws in d.values { t.removeAll() dfs(ws) From 376d4006c2ff17d17e777de5158bc57e4e6c1ce9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 16 May 2024 15:37:56 +0800 Subject: [PATCH 3/5] Update Solution.swift --- lcci/17.25.Word Rectangle/Solution.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lcci/17.25.Word Rectangle/Solution.swift b/lcci/17.25.Word Rectangle/Solution.swift index 4973031569dfa..e5cd4c2ceece3 100644 --- a/lcci/17.25.Word Rectangle/Solution.swift +++ b/lcci/17.25.Word Rectangle/Solution.swift @@ -1,7 +1,7 @@ class Trie { var children = [Trie?](repeating: nil, count: 26) var isEnd = false - + func insert(_ word: String) { var node = self for c in word { @@ -18,18 +18,18 @@ class Trie { class Solution { private var maxL = 0 private var maxS = 0 - private var ans: [String]? + private var ans: [String] = [] private var trie = Trie() private var t = [String]() - func maxRectangle(_ words: [String]) -> [String]? { + func maxRectangle(_ words: [String]) -> [String] { var d = [Int: [String]]() for word in words { maxL = max(maxL, word.count) trie.insert(word) d[word.count, default: []].append(word) } - + for ws in d.values { t.removeAll() dfs(ws) @@ -75,4 +75,4 @@ class Solution { } return result } -} \ No newline at end of file +} From 76d4933f2c149c03c84b5d4358a8816fbae5f9a0 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 16 May 2024 15:38:13 +0800 Subject: [PATCH 4/5] Update README_EN.md --- lcci/17.25.Word Rectangle/README_EN.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcci/17.25.Word Rectangle/README_EN.md b/lcci/17.25.Word Rectangle/README_EN.md index 8bc84f25a39a3..dfad0fbb4761d 100644 --- a/lcci/17.25.Word Rectangle/README_EN.md +++ b/lcci/17.25.Word Rectangle/README_EN.md @@ -375,11 +375,11 @@ class Trie { class Solution { private var maxL = 0 private var maxS = 0 - private var ans: [String]? + private var ans: [String] = [] private var trie = Trie() private var t = [String]() - func maxRectangle(_ words: [String]) -> [String]? { + func maxRectangle(_ words: [String]) -> [String] { var d = [Int: [String]]() for word in words { maxL = max(maxL, word.count) From 19642a97b0491689a74aad7b62d0ee3f751e18cc Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 16 May 2024 15:38:47 +0800 Subject: [PATCH 5/5] Update README.md --- lcci/17.25.Word Rectangle/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcci/17.25.Word Rectangle/README.md b/lcci/17.25.Word Rectangle/README.md index 8a7aeb1bc7b0e..bc5fcb606eb2a 100644 --- a/lcci/17.25.Word Rectangle/README.md +++ b/lcci/17.25.Word Rectangle/README.md @@ -370,11 +370,11 @@ class Trie { class Solution { private var maxL = 0 private var maxS = 0 - private var ans: [String]? + private var ans: [String] = [] private var trie = Trie() private var t = [String]() - func maxRectangle(_ words: [String]) -> [String]? { + func maxRectangle(_ words: [String]) -> [String] { var d = [Int: [String]]() for word in words { maxL = max(maxL, word.count)