-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-justification.go
114 lines (105 loc) · 2.65 KB
/
text-justification.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"strings"
)
// source: https://leetcode.com/problems/text-justification/
func fullJustify(words []string, maxWidth int) []string {
ans := make([]string, 0)
textWords := make([][]string, 0)
textWordsLens := make([]int, 0)
i := 0
// collecting words
for i < len(words) {
textWords = append(textWords, make([]string, 0))
ln := len(textWords) - 1
strLen := 0
for i < len(words) && strLen+len(textWords[ln])-1+len(words[i]) < maxWidth {
textWords[ln] = append(textWords[ln], words[i])
strLen += len(words[i])
i++
}
textWordsLens = append(textWordsLens, strLen)
}
// counting spaces and building strings
for i = 0; i < len(textWords); i++ {
// if there is only one word or this is the last row, then left alignemnt
if len(textWords[i]) == 1 || i == len(textWords)-1 {
str := strings.Join(textWords[i], " ")
for len(str) < maxWidth {
str += " "
}
ans = append(ans, str)
continue
}
strSpaces := make([]string, len(textWords[i]))
strSpacesNum := maxWidth - textWordsLens[i]
for j := 0; strSpacesNum > 0; strSpacesNum-- {
strSpaces[j] += " "
j++
if j == len(strSpaces)-1 {
j = 0
}
}
// building the string
str := ""
for j := range textWords[i] {
str += textWords[i][j] + strSpaces[j]
}
ans = append(ans, str)
}
return ans
}
func main() {
testCases := []struct {
words []string
maxWidth int
want []string
}{
{
words: []string{"This", "is", "an", "example", "of", "text", "justification."},
maxWidth: 16,
want: []string{
"This is an",
"example of text",
"justification. ",
},
},
{
words: []string{"What", "must", "be", "acknowledgment", "shall", "be"},
maxWidth: 16,
want: []string{
"What must be",
"acknowledgment ",
"shall be ",
},
},
{
words: []string{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"},
maxWidth: 20,
want: []string{
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do ",
},
},
}
successes := 0
for _, tc := range testCases {
x := fullJustify(tc.words, tc.maxWidth)
status := "ERROR"
if fmt.Sprint(x) == fmt.Sprint(tc.want) {
status = "OK"
successes++
}
fmt.Println(status, " Expected: ", tc.want, " Actual: ", x)
}
if l := len(testCases); successes == len(testCases) {
fmt.Printf("===\nSUCCESS: %d of %d tests ended successfully\n", successes, l)
} else {
fmt.Printf("===\nFAIL: %d tests failed\n", l-successes)
}
}