Skip to content

Commit 23bf81d

Browse files
committed
Refactor solution to Text Justification
1 parent ae09477 commit 23bf81d

File tree

1 file changed

+42
-47
lines changed

1 file changed

+42
-47
lines changed

String/TextJustification.swift

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,67 @@
77

88
class TextJustification {
99
func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] {
10-
var res = [String]()
11-
var last = 0, currentLineLength = 0
12-
10+
var res = [String](), rowStart = 0, currentLen = 0
11+
1312
for (i, word) in words.enumerated() {
14-
if currentLineLength + word.count + (i - last) > maxWidth {
15-
16-
res.append(buildLine(words, last, i - 1, maxWidth, currentLineLength))
17-
18-
last = i
19-
currentLineLength = 0
13+
14+
if currentLen + word.count + (i - rowStart) > maxWidth {
15+
res.append(buildRow(rowStart, i - 1, words, maxWidth, currentLen))
16+
17+
rowStart = i
18+
currentLen = 0
2019
}
21-
22-
currentLineLength += word.count
20+
21+
currentLen += word.count
2322
}
24-
25-
res.append(buildLastLine(words, last, words.count - 1, maxWidth))
26-
23+
24+
res.append(buildLastRow(rowStart, words, maxWidth))
25+
2726
return res
2827
}
29-
30-
fileprivate func buildLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int, _ currentLineLength: Int) -> String {
31-
var line = ""
32-
var extraSpaceNum = 0, spaceNum = 0
33-
28+
29+
private func buildRow(_ start: Int, _ end: Int, _ words: [String], _ maxWidth: Int, _ currentLen: Int) -> String {
30+
var res = "", spaceNum = 0, extraSpaceNum = 0
31+
3432
if end > start {
35-
extraSpaceNum = (maxWidth - currentLineLength) % (end - start)
36-
spaceNum = (maxWidth - currentLineLength) / (end - start)
33+
spaceNum = (maxWidth - currentLen) / (end - start)
34+
extraSpaceNum = (maxWidth - currentLen) % (end - start)
3735
} else {
38-
spaceNum = maxWidth - currentLineLength
36+
spaceNum = maxWidth - currentLen
3937
}
40-
38+
4139
for i in start...end {
42-
line.append(words[i])
43-
40+
res += words[i]
41+
4442
if start != end && i == end {
4543
break
46-
}
47-
48-
for _ in 0..<spaceNum {
49-
line.append(" ")
5044
}
51-
45+
46+
res.append(String(repeating: " ", count: spaceNum))
5247
if extraSpaceNum > 0 {
53-
line.append(" ")
48+
res.append(" ")
5449
extraSpaceNum -= 1
5550
}
5651
}
57-
58-
return line
52+
53+
return res
5954
}
60-
61-
fileprivate func buildLastLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int) -> String {
62-
var line = ""
63-
64-
for i in start...end {
65-
line.append(words[i])
66-
67-
if i < end {
68-
line.append(" ")
55+
56+
private func buildLastRow(_ start: Int, _ words: [String], _ maxWidth: Int) -> String {
57+
var res = ""
58+
59+
for i in start..<words.count {
60+
res += words[i]
61+
62+
if i + 1 < words.count {
63+
res.append(" ")
6964
}
7065
}
71-
72-
while line.count < maxWidth {
73-
line.append(" ")
66+
67+
if res.count < maxWidth {
68+
res.append(String(repeating: " ", count: maxWidth - res.count))
7469
}
75-
76-
return line
70+
71+
return res
7772
}
7873
}

0 commit comments

Comments
 (0)