Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 386
Good morning! Here's your coding interview problem for today.

This problem was asked by Twitter.

Given a string, sort it in decreasing order based on the frequency of characters. If there are multiple possible solutions, return any of them.

For example, given the string `tweet`, return `tteew`. `eettw` would also be acceptable.

Pull request description:
Day386
Resolves #778.

Patch:
diff --git a/README.md b/README.md
index ac83cca..d5a0b0b 100644
--- a/README.md
+++ b/README.md
@@ -381,4 +381,5 @@ problems from
 * [Day 383](https://github.com/vaskoz/dailycodingproblem-go/issues/772)
 * [Day 384](https://github.com/vaskoz/dailycodingproblem-go/issues/776)
 * [Day 385](https://github.com/vaskoz/dailycodingproblem-go/issues/774)
+* [Day 386](https://github.com/vaskoz/dailycodingproblem-go/issues/778)
 
diff --git a/day386/problem.go b/day386/problem.go
new file mode 100644
index 0000000..7c19bd0
--- /dev/null
+++ b/day386/problem.go
@@ -0,0 +1,28 @@
+package day386
+
+import "sort"
+
+// SortStringByCharFreq sorts the string by character
+// frequency with the most frequent letters first.
+// For a tie-breaker, they're sorted in reverse alphabetical
+// order.
+func SortStringByCharFreq(s string) string {
+	freq := make(map[rune]int)
+	runes := []rune(s)
+
+	for _, r := range runes {
+		freq[r]++
+	}
+
+	sort.Slice(runes, func(i, j int) bool {
+		if f1, f2 := freq[runes[i]], freq[runes[j]]; f1 > f2 {
+			return true
+		} else if f1 == f2 {
+			return runes[i] > runes[j]
+		}
+
+		return false
+	})
+
+	return string(runes)
+}


Test patch:
diff --git a/day386/problem_test.go b/day386/problem_test.go
new file mode 100644
index 0000000..fd6f5a3
--- /dev/null
+++ b/day386/problem_test.go
@@ -0,0 +1,29 @@
+package day386
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	input, expected string
+}{
+	{"tweet", "tteew"},
+	{"happiness", "ssppnihea"},
+}
+
+func TestSortStringByCharFreq(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if res := SortStringByCharFreq(tc.input); res != tc.expected {
+			t.Errorf("Expected %v, got %v", tc.expected, res)
+		}
+	}
+}
+
+func BenchmarkSortStringByCharFreq(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			SortStringByCharFreq(tc.input)
+		}
+	}
+}