forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorganize-string.go
executable file
·60 lines (49 loc) · 1.07 KB
/
reorganize-string.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
package problem0767
import (
"sort"
)
func reorganizeString(s string) string {
n := len(s)
bs := []byte(s)
count := make([][2]int, 26)
maxCount := 0
for _, b := range bs {
count[b-'a'][0]++ // 0 位放字母的个数
count[b-'a'][1] = int(b - 'a') // 1 位字母本身
maxCount = max(maxCount, count[b-'a'][0])
}
// 利用个数最多的元素,进行快速判断。
if 2*maxCount > n+1 {
return ""
}
sort.Slice(count, func(i, j int) bool {
if count[i][0] == count[j][0] {
// 字母个数相同的时候,ascii 码小的在前面
return count[i][1] < count[j][1]
}
// 个数多的字母排在前面
return count[i][0] > count[j][0]
})
idx := 0
for i := 0; i < 26 && count[i][0] > 0; i++ {
b := byte('a' + count[i][1])
for count[i][0] > 0 {
bs[idx] = b
count[i][0]--
// 填写的时候,留下间隔
// 避免相同的字母相邻
idx += 2
if idx >= n {
// 到头了以后,从 1 位重新填写
idx = 1
}
}
}
return string(bs)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}