-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRansomNote.cs
40 lines (33 loc) · 964 Bytes
/
RansomNote.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//https://leetcode.com/problems/ransom-note/
namespace LeetCode.Problems;
public sealed class RansomNote : ProblemBase
{
[Theory]
[ClassData(typeof(RansomNote))]
public override void Test(object[] data) => base.Test(data);
protected override void AddTestCases()
=> Add(it => it.Param("a").Param("b").Result(false))
.Add(it => it.Param("aa").Param("ab").Result(false))
.Add(it => it.Param("aa").Param("aab").Result(true))
;
private bool Solution(string ransomNote, string magazine)
{
if (magazine.Length < ransomNote.Length)
{
return false;
}
var note = new int[26];
foreach(var letter in magazine)
{
note[letter - 'a']++;
};
foreach(var letter in ransomNote)
{
if (--note[letter - 'a'] < 0)
{
return false;
}
}
return true;
}
}