451. Sort Characters By Frequency

This commit is contained in:
Пытков Роман
2024-02-07 21:53:40 +03:00
parent d15aa071c8
commit bfb8311fe7
5 changed files with 89 additions and 32 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>_451._Sort_Characters_By_Frequency</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,4 @@
using _451._Sort_Characters_By_Frequency;
var sol = new Solution();
Console.WriteLine(sol.FrequencySort("gbngjaaaaaaaafsdgnjk;fdsnjkg;fds"));

View File

@@ -0,0 +1,16 @@
namespace _451._Sort_Characters_By_Frequency;
public class Solution
{
public string FrequencySort(string s)
{
Dictionary<char, int> dictionary = [];
foreach (var c in s.Where(c => !dictionary.TryAdd(c, 1)))
dictionary[c]++;
var list = dictionary.ToList();
return String.Join("",
list.OrderByDescending(element => element.Value)
.Select(element => new string(element.Key, element.Value)));
}
}