fast solution 451

This commit is contained in:
Пытков Роман
2024-02-07 22:08:26 +03:00
parent bfb8311fe7
commit c47d2185b5
3 changed files with 55 additions and 30 deletions

View File

@@ -0,0 +1,26 @@
using System.Text;
namespace _451._Sort_Characters_By_Frequency.Fast;
public class Solution
{
private static int Comparer((char, int) el1, (char, int) el2)
{
return el2.Item2.CompareTo(el1.Item2);
}
public string FrequencySort(string s)
{
var count = new (char, int)[128];
for (var ch = (char)0; ch < count.Length; ch++)
count[ch].Item1 = ch;
foreach (var c in s)
count[c].Item2++;
var sb = new StringBuilder();
Array.Sort(count, Comparer);
for (var ch = (char)0; ch < count.Length; ch++)
while (count[ch].Item2-- > 0)
sb.Append(count[ch].Item1);
return sb.ToString();
}
}