aboutsummaryrefslogtreecommitdiffstats
path: root/451
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-12-03 14:34:24 +0000
committerterminaldweller <thabogre@gmail.com>2022-12-03 14:34:24 +0000
commitce79562ebcdd5cac9a520dc411f6192184342f43 (patch)
tree508b9b207f342a2c83ce3318de7170cf5e88262a /451
parent1657 (diff)
downloadleetcode-ce79562ebcdd5cac9a520dc411f6192184342f43.tar.gz
leetcode-ce79562ebcdd5cac9a520dc411f6192184342f43.zip
451
Diffstat (limited to '451')
-rw-r--r--451/go.mod3
-rw-r--r--451/main.go54
2 files changed, 57 insertions, 0 deletions
diff --git a/451/go.mod b/451/go.mod
new file mode 100644
index 0000000..b3a3c4d
--- /dev/null
+++ b/451/go.mod
@@ -0,0 +1,3 @@
+module 451
+
+go 1.18
diff --git a/451/main.go b/451/main.go
new file mode 100644
index 0000000..f025ce7
--- /dev/null
+++ b/451/main.go
@@ -0,0 +1,54 @@
+package main
+
+import (
+ "fmt"
+ "sort"
+)
+
+func frequencySort(s string) string {
+ dict := func(s string) map[int]int {
+ dict := make(map[int]int)
+
+ for i := 0; i < len(s); i++ {
+ if _, ok := dict[int(s[i])]; ok {
+ dict[int(s[i])]++
+ } else {
+ dict[int(s[i])] = 1
+ }
+ }
+
+ return dict
+ }(s)
+
+ type kv struct {
+ key int
+ val int
+ }
+
+ var ss []kv
+
+ for k, v := range dict {
+ ss = append(ss, kv{k, v})
+ }
+
+ sort.Slice(ss, func(i, j int) bool {
+ return ss[i].val > ss[j].val
+ })
+
+ bytes := make([]byte, len(s), len(s))
+ counter := 0
+ for _, kv := range ss {
+ for i := 0; i < kv.val; i++ {
+ bytes[counter] = byte(kv.key)
+ counter++
+ }
+ }
+
+ return string(bytes)
+}
+
+func main() {
+ fmt.Println(frequencySort("tree"))
+ fmt.Println(frequencySort("cccaaa"))
+ fmt.Println(frequencySort("Aabb"))
+}