Skip to content

Commit 8640250

Browse files
authored
feat: add swift implementation to lcci problem: No.17.25 (doocs#2823)
1 parent a2da1d6 commit 8640250

File tree

3 files changed

+240
-0
lines changed

3 files changed

+240
-0
lines changed

lcci/17.25.Word Rectangle/README.md

+81
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,87 @@ func maxRectangle(words []string) (ans []string) {
349349
}
350350
```
351351

352+
```swift
353+
class Trie {
354+
var children = [Trie?](repeating: nil, count: 26)
355+
var isEnd = false
356+
357+
func insert(_ word: String) {
358+
var node = self
359+
for c in word {
360+
let index = Int(c.asciiValue! - Character("a").asciiValue!)
361+
if node.children[index] == nil {
362+
node.children[index] = Trie()
363+
}
364+
node = node.children[index]!
365+
}
366+
node.isEnd = true
367+
}
368+
}
369+
370+
class Solution {
371+
private var maxL = 0
372+
private var maxS = 0
373+
private var ans: [String] = []
374+
private var trie = Trie()
375+
private var t = [String]()
376+
377+
func maxRectangle(_ words: [String]) -> [String] {
378+
var d = [Int: [String]]()
379+
for word in words {
380+
maxL = max(maxL, word.count)
381+
trie.insert(word)
382+
d[word.count, default: []].append(word)
383+
}
384+
385+
for ws in d.values {
386+
t.removeAll()
387+
dfs(ws)
388+
}
389+
return ans
390+
}
391+
392+
private func dfs(_ ws: [String]) {
393+
guard let first = ws.first, first.count * maxL > maxS, t.count < maxL else { return }
394+
for w in ws {
395+
t.append(w)
396+
let st = check(t)
397+
switch st {
398+
case 0:
399+
t.removeLast()
400+
case 1:
401+
if maxS < t.count * t[0].count {
402+
maxS = t.count * t[0].count
403+
ans = t
404+
}
405+
dfs(ws)
406+
t.removeLast()
407+
default:
408+
dfs(ws)
409+
t.removeLast()
410+
}
411+
}
412+
}
413+
414+
private func check(_ mat: [String]) -> Int {
415+
let m = mat.count, n = mat[0].count
416+
var result = 1
417+
for j in 0..<n {
418+
var node = trie
419+
for i in 0..<m {
420+
let index = Int(mat[i][mat[i].index(mat[i].startIndex, offsetBy: j)].asciiValue! - Character("a").asciiValue!)
421+
guard let nextNode = node.children[index] else { return 0 }
422+
node = nextNode
423+
}
424+
if !node.isEnd {
425+
result = 2
426+
}
427+
}
428+
return result
429+
}
430+
}
431+
```
432+
352433
<!-- tabs:end -->
353434

354435
<!-- end -->

lcci/17.25.Word Rectangle/README_EN.md

+81
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,87 @@ func maxRectangle(words []string) (ans []string) {
354354
}
355355
```
356356

357+
```swift
358+
class Trie {
359+
var children = [Trie?](repeating: nil, count: 26)
360+
var isEnd = false
361+
362+
func insert(_ word: String) {
363+
var node = self
364+
for c in word {
365+
let index = Int(c.asciiValue! - Character("a").asciiValue!)
366+
if node.children[index] == nil {
367+
node.children[index] = Trie()
368+
}
369+
node = node.children[index]!
370+
}
371+
node.isEnd = true
372+
}
373+
}
374+
375+
class Solution {
376+
private var maxL = 0
377+
private var maxS = 0
378+
private var ans: [String] = []
379+
private var trie = Trie()
380+
private var t = [String]()
381+
382+
func maxRectangle(_ words: [String]) -> [String] {
383+
var d = [Int: [String]]()
384+
for word in words {
385+
maxL = max(maxL, word.count)
386+
trie.insert(word)
387+
d[word.count, default: []].append(word)
388+
}
389+
390+
for ws in d.values {
391+
t.removeAll()
392+
dfs(ws)
393+
}
394+
return ans
395+
}
396+
397+
private func dfs(_ ws: [String]) {
398+
guard let first = ws.first, first.count * maxL > maxS, t.count < maxL else { return }
399+
for w in ws {
400+
t.append(w)
401+
let st = check(t)
402+
switch st {
403+
case 0:
404+
t.removeLast()
405+
case 1:
406+
if maxS < t.count * t[0].count {
407+
maxS = t.count * t[0].count
408+
ans = t
409+
}
410+
dfs(ws)
411+
t.removeLast()
412+
default:
413+
dfs(ws)
414+
t.removeLast()
415+
}
416+
}
417+
}
418+
419+
private func check(_ mat: [String]) -> Int {
420+
let m = mat.count, n = mat[0].count
421+
var result = 1
422+
for j in 0..<n {
423+
var node = trie
424+
for i in 0..<m {
425+
let index = Int(mat[i][mat[i].index(mat[i].startIndex, offsetBy: j)].asciiValue! - Character("a").asciiValue!)
426+
guard let nextNode = node.children[index] else { return 0 }
427+
node = nextNode
428+
}
429+
if !node.isEnd {
430+
result = 2
431+
}
432+
}
433+
return result
434+
}
435+
}
436+
```
437+
357438
<!-- tabs:end -->
358439

359440
<!-- end -->
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class Trie {
2+
var children = [Trie?](repeating: nil, count: 26)
3+
var isEnd = false
4+
5+
func insert(_ word: String) {
6+
var node = self
7+
for c in word {
8+
let index = Int(c.asciiValue! - Character("a").asciiValue!)
9+
if node.children[index] == nil {
10+
node.children[index] = Trie()
11+
}
12+
node = node.children[index]!
13+
}
14+
node.isEnd = true
15+
}
16+
}
17+
18+
class Solution {
19+
private var maxL = 0
20+
private var maxS = 0
21+
private var ans: [String] = []
22+
private var trie = Trie()
23+
private var t = [String]()
24+
25+
func maxRectangle(_ words: [String]) -> [String] {
26+
var d = [Int: [String]]()
27+
for word in words {
28+
maxL = max(maxL, word.count)
29+
trie.insert(word)
30+
d[word.count, default: []].append(word)
31+
}
32+
33+
for ws in d.values {
34+
t.removeAll()
35+
dfs(ws)
36+
}
37+
return ans
38+
}
39+
40+
private func dfs(_ ws: [String]) {
41+
guard let first = ws.first, first.count * maxL > maxS, t.count < maxL else { return }
42+
for w in ws {
43+
t.append(w)
44+
let st = check(t)
45+
switch st {
46+
case 0:
47+
t.removeLast()
48+
case 1:
49+
if maxS < t.count * t[0].count {
50+
maxS = t.count * t[0].count
51+
ans = t
52+
}
53+
dfs(ws)
54+
t.removeLast()
55+
default:
56+
dfs(ws)
57+
t.removeLast()
58+
}
59+
}
60+
}
61+
62+
private func check(_ mat: [String]) -> Int {
63+
let m = mat.count, n = mat[0].count
64+
var result = 1
65+
for j in 0..<n {
66+
var node = trie
67+
for i in 0..<m {
68+
let index = Int(mat[i][mat[i].index(mat[i].startIndex, offsetBy: j)].asciiValue! - Character("a").asciiValue!)
69+
guard let nextNode = node.children[index] else { return 0 }
70+
node = nextNode
71+
}
72+
if !node.isEnd {
73+
result = 2
74+
}
75+
}
76+
return result
77+
}
78+
}

0 commit comments

Comments
 (0)