Day5
This commit is contained in:
11
409. Longest Palindrome/409. Longest Palindrome.csproj
Normal file
11
409. Longest Palindrome/409. Longest Palindrome.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_409._Longest_Palindrome</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
10
409. Longest Palindrome/Program.cs
Normal file
10
409. Longest Palindrome/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace _409._Longest_Palindrome
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(new Solution().LongestPalindrome("bb"));
|
||||
}
|
||||
}
|
||||
}
|
||||
25
409. Longest Palindrome/Solution.cs
Normal file
25
409. Longest Palindrome/Solution.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace _409._Longest_Palindrome;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public int LongestPalindrome(string s)
|
||||
{
|
||||
Dictionary<char, int> dict = new Dictionary<char, int>();
|
||||
foreach(var ch in s)
|
||||
{
|
||||
if(dict.ContainsKey(ch))
|
||||
dict[ch]++;
|
||||
else
|
||||
dict.Add(ch, 1);
|
||||
}
|
||||
int sum = 0;
|
||||
bool hasOdd = false;
|
||||
foreach(var v in dict.Values)
|
||||
{
|
||||
sum += v / 2;
|
||||
if(v % 2 == 1)
|
||||
hasOdd = true;
|
||||
}
|
||||
return sum*2 + (hasOdd ? 1 : 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user