Skip to content

Commit 198368a

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Hash Tables: Ransom Note. Solved ✓.
1 parent 6405abb commit 198368a

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed
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`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]
3+
*/
4+
5+
const __YES__ = 'Yes';
6+
const __NO__ = 'No';
7+
8+
export function checkMagazineCompute(magazine, note) {
9+
const dictionary = {};
10+
11+
for (const word of magazine) {
12+
if (word in dictionary) {
13+
dictionary[word] += 1;
14+
} else {
15+
dictionary[word] = 1;
16+
}
17+
}
18+
19+
for (const word of note) {
20+
if (word in dictionary && dictionary[word] > 0) {
21+
dictionary[word] -= 1;
22+
} else {
23+
return false;
24+
}
25+
}
26+
27+
return true;
28+
}
29+
30+
export function checkMagazine(magazine, note) {
31+
return checkMagazineCompute(magazine, note) ? __YES__ : __NO__;
32+
}
33+
34+
export default { checkMagazine };
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { checkMagazine } from './ctci-ransom-note.js';
5+
6+
const TEST_CASES = [
7+
{
8+
title: 'Sample Test Case 0',
9+
magazine: ['give', 'me', 'one', 'grand', 'today', 'night'],
10+
note: ['give', 'one', 'grand', 'today'],
11+
expected: 'Yes'
12+
},
13+
{
14+
title: 'Sample Test Case 1',
15+
magazine: ['two', 'times', 'three', 'is', 'not', 'four'],
16+
note: ['two', 'times', 'two', 'is', 'four'],
17+
expected: 'No'
18+
},
19+
{
20+
title: 'Sample Test',
21+
magazine: ['two', 'two', 'times', 'three', 'is', 'not', 'four'],
22+
note: ['two', 'times', 'two', 'is', 'four'],
23+
expected: 'Yes'
24+
}
25+
];
26+
describe('ctci_ransom_note', () => {
27+
it('checkMagazine test cases', () => {
28+
expect.assertions(3);
29+
30+
TEST_CASES.forEach((value) => {
31+
const answer = checkMagazine(value.magazine, value.note);
32+
33+
console.debug(
34+
`checkMagazine(${value.magazine}, ${value.note}) solution found: ${answer}`
35+
);
36+
37+
expect(answer).toStrictEqual(value.expected);
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)