Skip to content

Commit 9050ac1

Browse files
authored
Merge pull request #218 from sir-gon/feature/ctci-ransom-note
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: H…
2 parents 19b92c1 + 80e4725 commit 9050ac1

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
import util.Log;
7+
8+
/**
9+
* RansomNote.
10+
*
11+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]
12+
*/
13+
public class RansomNote {
14+
/**
15+
* InvalidValueException.
16+
*/
17+
public class InvalidValueException extends Exception {
18+
// constructor for the InvalidAgeException class
19+
public InvalidValueException(String msg) {
20+
Log.error(msg);
21+
}
22+
}
23+
24+
protected RansomNote() {
25+
}
26+
27+
private static final String YES = "Yes";
28+
private static final String NO = "No";
29+
30+
/**
31+
* checkMagazineCompute.
32+
*/
33+
public static boolean checkMagazineCompute(List<String> magazine, List<String> note) {
34+
Map<String, Integer> dictionary = new HashMap<>();
35+
36+
for (String word : magazine) {
37+
if (dictionary.putIfAbsent(word, 1) != null) {
38+
Integer currentValue = dictionary.get(word);
39+
dictionary.put(word, currentValue + 1);
40+
}
41+
}
42+
43+
for (String word : note) {
44+
try {
45+
dictionary.put(word, dictionary.get(word) - 1);
46+
if (dictionary.get(word) < 0) {
47+
throw new RansomNote().new InvalidValueException("Value can't go below 0");
48+
}
49+
} catch (Exception e) {
50+
return false;
51+
}
52+
}
53+
54+
return true;
55+
}
56+
57+
/**
58+
* checkMagazine.
59+
*/
60+
public static String checkMagazine(List<String> magazine, List<String> note) {
61+
return checkMagazineCompute(magazine, note) ? YES : NO;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
@TestInstance(Lifecycle.PER_CLASS)
14+
class RansomNoteTest {
15+
16+
public static class RansomNoteTestCase {
17+
public String title;
18+
public List<String> magazine;
19+
public List<String> note;
20+
public String expected;
21+
}
22+
23+
List<RansomNoteTestCase> testCases;
24+
25+
@BeforeAll
26+
public void setup() throws IOException {
27+
String path = String.join("/", "hackerrank",
28+
"interview_preparation_kit",
29+
"dictionaries_and_hashmaps",
30+
"ctci_ransom_note.testcases.json");
31+
32+
this.testCases = JsonLoader.loadJson(path, RansomNoteTestCase.class);
33+
}
34+
35+
@Test void testArrayManipulation() {
36+
for (RansomNoteTestCase test : testCases) {
37+
String solutionFound = RansomNote.checkMagazine(test.magazine, test.note);
38+
39+
assertEquals(test.expected, solutionFound,
40+
String.format("%s(%s, %s) answer must be: %s",
41+
"CrushOptimized.arrayManipulation",
42+
test.magazine,
43+
test.note,
44+
test.expected
45+
)
46+
);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"magazine": ["give", "me", "one", "grand", "today", "night"],
5+
"note": ["give", "one", "grand", "today"],
6+
"expected": "Yes"
7+
},
8+
{
9+
"title": "Sample Test Case 1",
10+
"magazine": ["two", "times", "three", "is", "not", "four"],
11+
"note": ["two", "times", "two", "is", "four"],
12+
"expected": "No"
13+
},
14+
{
15+
"title": "Sample Test",
16+
"magazine": ["two", "two", "times", "three", "is", "not", "four"],
17+
"note": ["two", "times", "two", "is", "four"],
18+
"expected": "Yes"
19+
}
20+
]
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` `#dictionaries` `#hashmaps`
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)