原题
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
‘e’ appears twice while ‘r’ and ‘t’ both appear once.
So ‘e’ must appear before both ‘r’ and ‘t’. Therefore “eetr” is also a valid answer.
Example 2:
Input:
“cccaaa”
Output:
“cccaaa”
Explanation:
Both ‘c’ and ‘a’ appear three times, so “aaaccc” is also a valid answer.
Note that “cacaca” is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
代码实现
无需排序版
public string FrequencySort(string s)
{
Dictionary<char, int> freq = new Dictionary<char, int>();
string[] buckets = new string[s.Length+1];
//count char frequency
foreach (var c in s)
{
if (!freq.ContainsKey(c))
freq.Add(c, 1);
else freq[c]++;
}
//put char to buckets
foreach (var item in freq)
{
int n = item.Value;
char c = item.Key;
StringBuilder builder = new StringBuilder();
buckets[n] += builder.Append(c, n).ToString();
}
StringBuilder builder2 = new StringBuilder();
//form string
for (int i = s.Length; i > 0; i--)
{
if (buckets[i] != null)
builder2.Append(buckets[i]);
}
return builder2.ToString();
}
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp