Day6. Namespace style

This commit is contained in:
Electrominch
2022-10-07 16:36:27 +03:00
parent 62104da8f1
commit 5cae083979
51 changed files with 589 additions and 202 deletions

View 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>

View File

@@ -0,0 +1,9 @@
namespace _383._Ransom_Note;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().CanConstruct("a", "b"));
}
}

View 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;
}
}