-
Notifications
You must be signed in to change notification settings - Fork 481
/
1417.go
40 lines (36 loc) · 833 Bytes
/
1417.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
func reformat(s string) string {
digits := []rune{}
chars := []rune{}
for _, c := range s {
if c >= '0' && c <= '9' {
digits = append(digits, c)
} else {
chars = append(chars, c)
}
}
if abs(len(digits) - len(chars)) > 1 {
return ""
}
var res strings.Builder
flag := false
if len(digits) >= len(chars) {
flag = true
}
for i := 0; i < len(s); i++ {
if flag {
res.WriteRune(digits[len(digits) - 1])
digits = digits[:len(digits) - 1]
} else {
res.WriteRune(chars[len(chars) - 1])
chars = chars[:len(chars) - 1]
}
flag = !flag
}
return res.String()
}
func abs(a int) int {
if a < 0 {
return -a
}
return a
}