Skip to content

Commit f7608d0

Browse files
authored
Merge pull request #77 from sir-gon/feature/ctci-ransom-note
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: H…
2 parents 659ca17 + 2ca0bec commit f7608d0

File tree

3 files changed

+207
-0
lines changed
  • algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps
  • algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps
  • docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps

3 files changed

+207
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class RansomNoteTest
5+
{
6+
public class RansomNoteTestCase
7+
{
8+
public string title = "";
9+
public List<string> magazine = [];
10+
public List<string> note = [];
11+
public string expected = "";
12+
}
13+
14+
public class ArraysLeftRotationsTestCase
15+
{
16+
public List<int> input = []; public int d; public List<int> expected = [];
17+
}
18+
19+
private static readonly RansomNoteTestCase[] tests = [
20+
new()
21+
{
22+
title = "Sample Test Case 0",
23+
magazine = ["give", "me", "one", "grand", "today", "night"],
24+
note = ["give", "one", "grand", "today"],
25+
expected = "Yes"
26+
},
27+
new()
28+
{
29+
title = "Sample Test Case 1",
30+
magazine = ["two", "times", "three", "is", "not", "four"],
31+
note = ["two", "times", "two", "is", "four"],
32+
expected = "No"
33+
},
34+
new()
35+
{
36+
title = "Sample Test",
37+
magazine = ["two", "two", "times", "three", "is", "not", "four"],
38+
note = ["two", "times", "two", "is", "four"],
39+
expected = "Yes"
40+
},
41+
];
42+
43+
[TestMethod]
44+
public void testCheckMagazine()
45+
{
46+
string result;
47+
48+
foreach (RansomNoteTestCase test in tests)
49+
{
50+
result = RansomNote.checkMagazine(test.magazine, test.note);
51+
Assert.AreEqual(test.expected, result);
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Collections.Generic;
7+
8+
public class RansomNote
9+
{
10+
[ExcludeFromCodeCoverage]
11+
protected RansomNote() { }
12+
13+
private static readonly string __YES__ = "Yes";
14+
private static readonly string __NO__ = "No";
15+
16+
public static bool checkMagazineCompute(List<string> magazine, List<string> note)
17+
{
18+
Dictionary<string, int?> dictionary = [];
19+
20+
foreach (string word in magazine)
21+
{
22+
if (dictionary.TryGetValue(word, out int? value))
23+
{
24+
dictionary[word] += 1;
25+
}
26+
else
27+
{
28+
dictionary[word] = 1;
29+
}
30+
}
31+
32+
foreach (string word in note)
33+
{
34+
if (dictionary.TryGetValue(word, out int? value) && value > 0)
35+
{
36+
dictionary[word] -= 1;
37+
}
38+
else
39+
{
40+
return false;
41+
}
42+
}
43+
44+
return true;
45+
}
46+
47+
public static string checkMagazine(List<string> magazine, List<string> note)
48+
{
49+
return checkMagazineCompute(magazine, note) ? __YES__ : __NO__;
50+
}
51+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [Hash Tables: Ransom Notes](https://www.hackerrank.com/challenges/ctci-ransom-note)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingIntermediate`
5+
6+
Harold is a kidnapper who wrote a ransom note, but now he is worried it will be
7+
traced back to him through his handwriting. He found a magazine and wants to
8+
know if he can cut out whole words from it and use them to create an untraceable
9+
replica of his ransom note.
10+
The words in his note are case-sensitive and he must use only whole words
11+
available in the magazine.
12+
He cannot use substrings or concatenation to create the words he needs.
13+
14+
Given the words in the magazine and the words in the ransom note,
15+
print `Yes` if he can replicate his ransom note exactly using whole words
16+
from the magazine; otherwise, print `No`.
17+
18+
## Example
19+
20+
`magazine` = "attack at dawn" `note` = "Attack at dawn"
21+
22+
The magazine has all the right words, but there is a case mismatch.
23+
The answer is `No`.
24+
25+
## Function Description
26+
27+
Complete the checkMagazine function in the editor below.
28+
It must print `Yes` if the note can be formed using the magazine, or .
29+
30+
checkMagazine has the following parameters:
31+
32+
- `string magazine[m]`: the words in the magazine
33+
- `string note[n]`: the words in the ransom note
34+
35+
## Prints
36+
37+
- string: either or , no return value is expected
38+
39+
## Input Format
40+
41+
The first line contains two space-separated integers, `m` and `n`,
42+
the numbers of words in the and the , respectively.
43+
44+
The second line contains `m` space-separated strings, each `magazine[i]`.
45+
46+
The third line contains `n` space-separated strings, each `node[i]`.
47+
48+
## Constraints
49+
50+
- $ 1 \leq m, n \leq 30000 $
51+
- $ 1 \leq $ length of `magazine[i]` and `note[i]` $ \leq 5 $
52+
- Each word consists of English alphabetic letters (i.e., `a` to `z` and `A` to `Z`).
53+
54+
## Sample Input 0
55+
56+
```text
57+
6 4
58+
give me one grand today night
59+
give one grand today
60+
```
61+
62+
## Sample Output 0
63+
64+
```text
65+
Yes
66+
```
67+
68+
## Sample Input 1
69+
70+
```text
71+
6 5
72+
two times three is not four
73+
two times two is four
74+
```
75+
76+
## Sample Output 1
77+
78+
```text
79+
No
80+
```
81+
82+
## Explanation 1
83+
84+
'two' only occurs once in the magazine.
85+
86+
## Sample Input 2
87+
88+
```text
89+
7 4
90+
ive got a lovely bunch of coconuts
91+
ive got some coconuts
92+
```
93+
94+
## Sample Output 2
95+
96+
```text
97+
No
98+
```
99+
100+
## Explanation 2
101+
102+
Harold's magazine is missing the word `some`.

0 commit comments

Comments
 (0)