-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0451-SortCharactersByFrequency.cs
43 lines (37 loc) · 1.28 KB
/
0451-SortCharactersByFrequency.cs
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
//-----------------------------------------------------------------------------
// Runtime: 108ms
// Memory Usage: 34.1 MB
// Link: https://leetcode.com/submissions/detail/343384693/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeetCode
{
public class _0451_SortCharactersByFrequency
{
public string FrequencySort(string s)
{
if (string.IsNullOrWhiteSpace(s)) return s;
var counts = new Dictionary<char, int>();
foreach (var ch in s)
{
if (!counts.ContainsKey(ch))
counts.Add(ch, 1);
else
counts[ch]++;
}
var maxCount = counts.Values.Max();
var buckets = new List<IList<char>>(maxCount + 1);
for (int i = 0; i <= maxCount; i++)
buckets.Add(new List<char>());
foreach (var ch in counts.Keys)
buckets[counts[ch]].Add(ch);
var sb = new StringBuilder();
for (int i = maxCount; i >= 1; i--)
foreach (var ch in buckets[i])
sb.Append(ch, i);
return sb.ToString();
}
}
}