Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polynomialspace/8272467ee6f67badcb78511bbd6e006d to your computer and use it in GitHub Desktop.
Save polynomialspace/8272467ee6f67badcb78511bbd6e006d to your computer and use it in GitHub Desktop.
import (
"sort"
"strings"
)
func frequencySort(s string) string {
abcfreq := make([]struct {
chr rune
cnt int
}, 128)
for _, r := range s {
abcfreq[r].chr = r
abcfreq[r].cnt++
}
sort.Slice(abcfreq, func(i int, j int) bool {
return abcfreq[i].cnt > abcfreq[j].cnt
})
sorted := strings.Builder{}
for _, val := range abcfreq {
sorted.WriteString(strings.Repeat(string(val.chr), val.cnt))
}
return sorted.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment