Skip to content

Instantly share code, notes, and snippets.

@jialinhuang00
Last active October 2, 2017 09:43
Show Gist options
  • Save jialinhuang00/f8de9b6679f5c28b00a03821c533a737 to your computer and use it in GitHub Desktop.
Save jialinhuang00/f8de9b6679f5c28b00a03821c533a737 to your computer and use it in GitHub Desktop.
/*-----------------------------------------------------------------------------
1.check if the 1st argument's words is existing at the 2nd argument.
2.the function is used to check if the latter is more apples than the former.
the reference is from UdemyCourse: LearningAlgorithmsInJavascriptFromScratch
-----------------------------------------------------------------------------*/
/*******************************************
NOTES:Time complexity, Big O notation()
Big O notation到底資料的增加是讓時間等比例增加還是指數增加…?
O(1) Constant
O(n) Linear runtime
O(n^2) Exponential runtime, such as two time recursives
O(log n) Logarithmic runtime, like binary search
*******************************************/
function harmlessRansomNote(noteText, magazineText) {
var noteArr = noteText.split(" ");
var magazineArr = magazineText.split(" ");
// build a dictionary
var magObj = {};
magazineArr.forEach(word => {
if (!magObj[word]) {
magObj[word] = 0;
}
magObj[word]++;
});
// burn a dictionary
var noteIsFound = true;
noteArr.forEach(word => {
if (magObj[word]) {
magObj[word]--;
if (magObj[word] < 0) noteIsFound = false;
} else {
noteIsFound = false;
}
});
return noteIsFound;
}
var note1 = "This is an apple";
var note2 = "This apple is really sweety apple";
var mag = "the apple on the tree this is an immature apple.";
harmlessRansomNote(note1, mag);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment