-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution1592.go
44 lines (38 loc) · 982 Bytes
/
solution1592.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
package solution1592
import (
"strings"
)
// ============================================================================
// 1592. Rearrange Spaces Between Words
// URL: https://leetcode.com/problems/rearrange-spaces-between-words/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_reorderSpaces-24 6889672 193.9 ns/op 136 B/op 7 allocs/op
PASS
*/
func reorderSpaces(text string) string {
if len(text) <= 1 {
return text
}
sb := strings.Builder{}
spaces := strings.Count(text, " ")
words := strings.Fields(text)
s1 := spaces
if len(words) > 1 {
s1 = spaces / (len(words) - 1)
}
a := 0
for i, word := range words {
sb.WriteString(word)
if i < len(words)-1 {
sb.WriteString(strings.Repeat(" ", s1))
a += s1
}
}
sb.WriteString(strings.Repeat(" ", spaces-a))
return sb.String()
}