forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextJustification.swift
78 lines (59 loc) · 2.25 KB
/
TextJustification.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Question Link: https://leetcode.com/problems/text-justification/
* Primary idea: Iterate the words, keep track of the index of first word and the length
* of the line. Insert spaces with fix spaces and extra spaces.
* Time Complexity: O(n), Space Complexity: O(n)
*/
class TextJustification {
func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] {
var res = [String]()
var last = 0, currentLineLength = 0
for (i, word) in words.enumerated() {
if currentLineLength + word.count + (i - last) > maxWidth {
res.append(buildLine(words, last, i - 1, maxWidth, currentLineLength))
last = i
currentLineLength = 0
}
currentLineLength += word.count
}
res.append(buildLastLine(words, last, words.count - 1, maxWidth))
return res
}
fileprivate func buildLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int, _ currentLineLength: Int) -> String {
var line = ""
var extraSpaceNum = 0, spaceNum = 0
if end > start {
extraSpaceNum = (maxWidth - currentLineLength) % (end - start)
spaceNum = (maxWidth - currentLineLength) / (end - start)
} else {
spaceNum = maxWidth - currentLineLength
}
for i in start...end {
line.append(words[i])
if start != end && i == end {
break
}
for _ in 0..<spaceNum {
line.append(" ")
}
if extraSpaceNum > 0 {
line.append(" ")
extraSpaceNum -= 1
}
}
return line
}
fileprivate func buildLastLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int) -> String {
var line = ""
for i in start...end {
line.append(words[i])
if i < end {
line.append(" ")
}
}
while line.count < maxWidth {
line.append(" ")
}
return line
}
}