Created
May 23, 2020 01:57
-
-
Save polynomialspace/8272467ee6f67badcb78511bbd6e006d to your computer and use it in GitHub Desktop.
Leetcode challenge 22/05/2020 https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3337/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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