Skip to content

Commit 3c326d8

Browse files
authored
Create harmless_ransom_note
1 parent e020ce4 commit 3c326d8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

algorithms/harmless_ransom_note

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Check if the provided paragraph have enough words to make the provided sentence.
2+
3+
function harmlessRansomNote(sentence, paragraph){
4+
5+
const sentenceArr = sentence.split(" ");
6+
const paragraphArr = paragraph.split(" ");
7+
let paragraphObj = {};
8+
let isSentencePossible = true;
9+
10+
paragraphArr.forEach(function(word){
11+
12+
if (!paragraphObj[word]) paragraphObj[word] = 0;
13+
paragraphObj[word]++;
14+
15+
});
16+
17+
18+
sentenceArr.forEach(function(word){
19+
20+
if (paragraphObj[word]) {
21+
22+
paragraphObj[word]--;
23+
if (paragraphObj[word] < 0) isSentencePossible = false;
24+
}
25+
26+
else isSentencePossible = false;
27+
28+
});
29+
30+
return isSentencePossible;
31+
32+
}
33+
34+
harmlessRansomNote('i am here', 'i am here ok. come on.. i am here');

0 commit comments

Comments
 (0)