patternjavascriptMinor
HackerRank "Ransom Note" challenge
Viewed 0 times
hackerranknoteransomchallenge
Problem
I have solved the Ransom Note
challenge on HackerRank using JavaScript / ECMAScript 6. The challenge goes like this:
A kidnapper wrote a ransom note but is worried it will be traced back to him. 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 whole words available in the magazine, meaning 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
Input Format
The first line contains two space-separated integers describing the respective values of (the number of words in the magazine) and (the number of words in the ransom note).
The second line contains space-separated strings denoting the words present in the magazine.
The third line contains space-separated strings denoting the words present in the ransom note.
My solution uses JavaScript
Note that the portion of code above
```
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
//////
challenge on HackerRank using JavaScript / ECMAScript 6. The challenge goes like this:
A kidnapper wrote a ransom note but is worried it will be traced back to him. 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 whole words available in the magazine, meaning 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.Input Format
The first line contains two space-separated integers describing the respective values of (the number of words in the magazine) and (the number of words in the ransom note).
The second line contains space-separated strings denoting the words present in the magazine.
The third line contains space-separated strings denoting the words present in the ransom note.
My solution uses JavaScript
Map objects (as suggested by the name of the challenge) and passes all tests with no timeouts. However, the solution itself is very literal, as in, it removes out each word in the ransom note from the magazine, unless the needed word is not available.Note that the portion of code above
ignore above this line is included with all challenges on the site, I left it in for the sake of completeness, so that anyone can copy-paste the whole block into the challenge page, if they wish.```
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
//////
Solution
This code reads like a story, very nice, though at times a bit verbose.
The value returned from
so you could drop the
In
Note that the map of word counts for
It's possible to simply loop over the words, updating the
and
You could use the
The value returned from
addValueOrIncrementCount is never used,so you could drop the
return statement.decrementCountIfPossible returns a count, or null if the count cannot be decremented. It would be simpler and more natural to use if it returned boolean, because the returned count is not used anyway.In
mapWordCounts the variable occurrences is declared but never used.Note that the map of word counts for
ransomMap is not really necessary.It's possible to simply loop over the words, updating the
magazineMap,and
return false whenever the magazineMap doesn't have the required word.You could use the
|| operator to simplify addValueOrIncrementCount:const addValueOrIncrementCount = (value, map) => {
map.set(value, 1 + (map.get(value) || 0))
}Code Snippets
const addValueOrIncrementCount = (value, map) => {
map.set(value, 1 + (map.get(value) || 0))
}Context
StackExchange Code Review Q#158745, answer score: 4
Revisions (0)
No revisions yet.