Day6. Namespace style
This commit is contained in:
11
383. Ransom Note/383. Ransom Note.csproj
Normal file
11
383. Ransom Note/383. Ransom Note.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_383._Ransom_Note</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
383. Ransom Note/Program.cs
Normal file
9
383. Ransom Note/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _383._Ransom_Note;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(new Solution().CanConstruct("a", "b"));
|
||||
}
|
||||
}
|
||||
36
383. Ransom Note/Solution.cs
Normal file
36
383. Ransom Note/Solution.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace _383._Ransom_Note;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public bool CanConstruct(string ransomNote, string magazine)
|
||||
{
|
||||
if (ransomNote.Length > magazine.Length)
|
||||
return false;
|
||||
Dictionary<char, int> note = CountChars(ransomNote);
|
||||
Dictionary<char, int> mag = CountChars(magazine);
|
||||
foreach (var kp in note)
|
||||
{
|
||||
if (mag.ContainsKey(kp.Key))
|
||||
{
|
||||
if (mag[kp.Key] < kp.Value)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Dictionary<char, int> CountChars(string str)
|
||||
{
|
||||
Dictionary<char, int> dict = new Dictionary<char, int>();
|
||||
foreach (char ch in str)
|
||||
{
|
||||
if (dict.ContainsKey(ch))
|
||||
dict[ch]++;
|
||||
else
|
||||
dict.Add(ch, 1);
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user