451. Sort Characters By Frequency
This commit is contained in:
@@ -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>
|
||||
4
451. Sort Characters By Frequency/Program.cs
Normal file
4
451. Sort Characters By Frequency/Program.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
using _451._Sort_Characters_By_Frequency;
|
||||
|
||||
var sol = new Solution();
|
||||
Console.WriteLine(sol.FrequencySort("gbngjaaaaaaaafsdgnjk;fdsnjkg;fds"));
|
||||
16
451. Sort Characters By Frequency/Solution.cs
Normal file
16
451. Sort Characters By Frequency/Solution.cs
Normal 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)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user