Skip to content

Commit 05fa438

Browse files
authored
Merge pull request #84 from sir-gon/develop
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: H…
2 parents e269662 + b5f5ca9 commit 05fa438

File tree

1 file changed

+15
-2
lines changed
  • algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps

1 file changed

+15
-2
lines changed

algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Collections.Generic;
77

8+
public class InvalidValueException : ApplicationException
9+
{
10+
// constructor for the InvalidAgeException class
11+
public InvalidValueException(string msg)
12+
{
13+
Console.WriteLine(msg);
14+
}
15+
}
16+
817
public class RansomNote
918
{
1019
[ExcludeFromCodeCoverage]
@@ -28,11 +37,15 @@ public static bool checkMagazineCompute(List<string> magazine, List<string> note
2837

2938
foreach (string word in note)
3039
{
31-
if (dictionary.TryGetValue(word, out int value) && value > 0)
40+
try
3241
{
3342
dictionary[word] -= 1;
43+
if (dictionary[word] < 0)
44+
{
45+
throw new InvalidValueException("Value can't go below 0");
46+
}
3447
}
35-
else
48+
catch
3649
{
3750
return false;
3851
}

0 commit comments

Comments
 (0)