-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsolution.go
58 lines (46 loc) · 851 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
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
package main
import (
"fmt"
"sort"
)
type segments []string
func (s segments) Len() int {
return len(s)
}
func (s segments) Less(i, j int) bool {
if len(s[i]) == len(s[j]) {
return s[i] < s[j]
}
return len(s[i]) > len(s[j])
}
func (s segments) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func frequencySort(s string) string {
freq := make([]int, 'z'+1)
for i := range s {
freq[s[i]]++
}
//strs := []string{"ttt", "ee", "r"}
var strs []string
for i := range freq {
if freq[i] == 0 {
continue
}
bytes := make([]byte, freq[i])
for j := range bytes {
bytes[j] = byte(i)
}
strs = append(strs, string(bytes))
}
// sort.Interface is implemented for segments
sort.Sort(segments(strs))
res := ""
for _, s := range strs {
res += s
}
return res
}
func main() {
fmt.Printf("%#v", frequencySort("tttree"))
}