We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First line we declare an unordered_map that contain a key (string) and a value (int).
Then for each words in the magazine we count the occurrence of this word.
Then we check for each words in the ransom if the map we created before contains an occurence of this word.
Two possibility that cause negative answer:
- We have reach the end of the array (map_word.end) without finding the word.
- The word has been already used in our ransom note more times than it is actualy present in the magazine.
Finally if we have reach the end of our algo without returning false, this means that we are good to go.
PS: I was actually quoting the solution of gerasiov
PS2: This is a naive solution
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
First line we declare an unordered_map that contain a key (string) and a value (int).
Then for each words in the magazine we count the occurrence of this word.
Then we check for each words in the ransom if the map we created before contains an occurence of this word.
Two possibility that cause negative answer:
- We have reach the end of the array (map_word.end) without finding the word.
- The word has been already used in our ransom note more times than it is actualy present in the magazine.
Finally if we have reach the end of our algo without returning false, this means that we are good to go.
PS: I was actually quoting the solution of gerasiov
PS2: This is a naive solution