Skip to content

Commit 971cae8

Browse files
authored
Merge pull request #325 from sir-gon/feature/ransom_note
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: H…
2 parents c498f87 + 6484be5 commit 971cae8

4 files changed

Lines changed: 216 additions & 0 deletions

File tree

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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import "fmt"
8+
9+
const __YES__ = "Yes"
10+
const __NO__ = "No"
11+
12+
func checkMagazineCompute(magazine []string, note []string) bool {
13+
dictionary := make(map[string]int)
14+
15+
for _, word := range magazine {
16+
dictionary[word]++
17+
}
18+
19+
for _, word := range note {
20+
if _, ok := dictionary[word]; ok && dictionary[word] > 0 {
21+
dictionary[word] -= 1
22+
} else {
23+
return false
24+
}
25+
}
26+
27+
return true
28+
}
29+
30+
func checkMagazineText(magazine []string, note []string) string {
31+
if checkMagazineCompute(magazine, note) {
32+
return __YES__
33+
}
34+
35+
return __NO__
36+
}
37+
38+
func checkMagazine(magazine []string, note []string) {
39+
fmt.Println(checkMagazineText(magazine, note))
40+
}
41+
42+
func CheckMagazineText(magazine []string, note []string) string {
43+
return checkMagazineText(magazine, note)
44+
}
45+
46+
func CheckMagazine(magazine []string, note []string) {
47+
checkMagazine(magazine, note)
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type RansomNoteTestCase struct {
13+
Magazine []string `json:"magazine"`
14+
Note []string `json:"note"`
15+
Expected string `json:"expected"`
16+
}
17+
18+
var RansomNoteTestCases []RansomNoteTestCase
19+
20+
// You can use testing.T, if you want to test the code without benchmarking
21+
func RansomNoteSetupSuite(t testing.TB) {
22+
wd, _ := os.Getwd()
23+
filepath := wd + "/ctci_ransom_note.testcases.json"
24+
t.Log("Setup test cases from JSON: ", filepath)
25+
26+
var _, err = utils.LoadJSON(filepath, &RansomNoteTestCases)
27+
if err != nil {
28+
t.Log(err)
29+
}
30+
}
31+
32+
func TestRansomNote(t *testing.T) {
33+
34+
RansomNoteSetupSuite(t)
35+
36+
for _, tt := range RansomNoteTestCases {
37+
testname := fmt.Sprintf("CheckMagazine(%v, %v) => %v \n", tt.Magazine, tt.Note, tt.Expected)
38+
t.Run(testname, func(t *testing.T) {
39+
40+
CheckMagazine(tt.Magazine, tt.Note)
41+
ans := CheckMagazineText(tt.Magazine, tt.Note)
42+
assert.Equal(t, tt.Expected, ans)
43+
})
44+
45+
}
46+
}
Lines changed: 20 additions & 0 deletions
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+
]

0 commit comments

Comments
 (0)