-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.go
29 lines (27 loc) · 884 Bytes
/
Solution.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
package Solution
import "strings"
func Solution(words []string, k int) []string {
result := make([]string, 0)
currWords := make([]string, 0)
currWordsLen := 0
for _, word := range words {
if currWordsLen+len(currWords)+len(word) > k {
if len(currWords) == 1 {
result = append(result, currWords[0]+strings.Repeat(" ", k-currWordsLen))
} else {
spaces := k - currWordsLen
spacesBetween, extras := spaces/(len(currWords)-1), spaces%(len(currWords)-1)
for i := 0; i < extras; i++ {
currWords[i] += " "
}
result = append(result, strings.Join(currWords, strings.Repeat(" ", spacesBetween)))
}
currWords = make([]string, 0)
currWordsLen = 0
}
currWords = append(currWords, word)
currWordsLen += len(word)
}
result = append(result, strings.Join(currWords, " ")+strings.Repeat(" ", k-currWordsLen-len(currWords)+1))
return result
}