Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# [Hash Tables: Ransom Notes](https://www.hackerrank.com/challenges/ctci-ransom-note)

- Difficulty: `#easy`
- Category: `#ProblemSolvingIntermediate`

Harold is a kidnapper who wrote a ransom note, but now he is worried it will be
traced back to him through his handwriting. He found a magazine and wants to
know if he can cut out whole words from it and use them to create an untraceable
replica of his ransom note.
The words in his note are case-sensitive and he must use only whole words
available in the magazine.
He cannot use substrings or concatenation to create the words he needs.

Given the words in the magazine and the words in the ransom note,
print `Yes` if he can replicate his ransom note exactly using whole words
from the magazine; otherwise, print `No`.

## Example

`magazine` = "attack at dawn" `note` = "Attack at dawn"

The magazine has all the right words, but there is a case mismatch.
The answer is `No`.

## Function Description

Complete the checkMagazine function in the editor below.
It must print `Yes` if the note can be formed using the magazine, or .

checkMagazine has the following parameters:

- `string magazine[m]`: the words in the magazine
- `string note[n]`: the words in the ransom note

## Prints

- string: either or , no return value is expected

## Input Format

The first line contains two space-separated integers, `m` and `n`,
the numbers of words in the and the , respectively.

The second line contains `m` space-separated strings, each `magazine[i]`.

The third line contains `n` space-separated strings, each `node[i]`.

## Constraints

- $ 1 \leq m, n \leq 30000 $
- $ 1 \leq $ length of `magazine[i]` and `note[i]` $ \leq 5 $
- Each word consists of English alphabetic letters (i.e., `a` to `z` and `A` to `Z`).

## Sample Input 0

```text
6 4
give me one grand today night
give one grand today
```

## Sample Output 0

```text
Yes
```

## Sample Input 1

```text
6 5
two times three is not four
two times two is four
```

## Sample Output 1

```text
No
```

## Explanation 1

'two' only occurs once in the magazine.

## Sample Input 2

```text
7 4
ive got a lovely bunch of coconuts
ive got some coconuts
```

## Sample Output 2

```text
No
```

## Explanation 2

Harold's magazine is missing the word `some`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]
*/

package hackerrank

import "fmt"

const __YES__ = "Yes"
const __NO__ = "No"

func checkMagazineCompute(magazine []string, note []string) bool {
dictionary := make(map[string]int)

for _, word := range magazine {
dictionary[word]++
}

for _, word := range note {
if _, ok := dictionary[word]; ok && dictionary[word] > 0 {
dictionary[word] -= 1
} else {
return false
}
}

return true
}

func checkMagazineText(magazine []string, note []string) string {
if checkMagazineCompute(magazine, note) {
return __YES__
}

return __NO__
}

func checkMagazine(magazine []string, note []string) {
fmt.Println(checkMagazineText(magazine, note))
}

func CheckMagazineText(magazine []string, note []string) string {
return checkMagazineText(magazine, note)
}

func CheckMagazine(magazine []string, note []string) {
checkMagazine(magazine, note)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hackerrank

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gon.cl/algorithms/utils"
)

type RansomNoteTestCase struct {
Magazine []string `json:"magazine"`
Note []string `json:"note"`
Expected string `json:"expected"`
}

var RansomNoteTestCases []RansomNoteTestCase

// You can use testing.T, if you want to test the code without benchmarking
func RansomNoteSetupSuite(t testing.TB) {
wd, _ := os.Getwd()
filepath := wd + "/ctci_ransom_note.testcases.json"
t.Log("Setup test cases from JSON: ", filepath)

var _, err = utils.LoadJSON(filepath, &RansomNoteTestCases)
if err != nil {
t.Log(err)
}
}

func TestRansomNote(t *testing.T) {

RansomNoteSetupSuite(t)

for _, tt := range RansomNoteTestCases {
testname := fmt.Sprintf("CheckMagazine(%v, %v) => %v \n", tt.Magazine, tt.Note, tt.Expected)
t.Run(testname, func(t *testing.T) {

CheckMagazine(tt.Magazine, tt.Note)
ans := CheckMagazineText(tt.Magazine, tt.Note)
assert.Equal(t, tt.Expected, ans)
})

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"title": "Sample Test Case 0",
"magazine": ["give", "me", "one", "grand", "today", "night"],
"note": ["give", "one", "grand", "today"],
"expected": "Yes"
},
{
"title": "Sample Test Case 1",
"magazine": ["two", "times", "three", "is", "not", "four"],
"note": ["two", "times", "two", "is", "four"],
"expected": "No"
},
{
"title": "Sample Test",
"magazine": ["two", "two", "times", "three", "is", "not", "four"],
"note": ["two", "times", "two", "is", "four"],
"expected": "Yes"
}
]