HACKERRANK INTERVIEW PREP KIT SOLUTIONS-
* WARM-UP CHALLEGES-
COUNTING VALLEYS
PROBLEM
* DATA STRUCTURES
ARRAY
LINKED LIST
STACK
QUEUE
HACKERRANK - CRACKING THE CODING INTERVIEW TUTORIALS SOLUTIONS IN CPP
HASH TABLES - RANSOM NOTE
Complete the checkMagazine function in the editor below. It must print 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, and , the numbers of words in the and the , respectively.
The second line contains space-separated strings, each .
The third line contains space-separated strings, each .
Constraints
- .
- Each word consists of English alphabetic letters (i.e., to and to ).
Sample Input 0
6 4
give me one grand today night
give one grand today
Sample Output 0
YesMY SOLUTION-void checkMagazine(vector<string> magazine, vector<string> note) { //Simple finding in the string magazine - passes some test cases
// for(int i = 0; i < note.size();){ // if(find(magazine.begin()+i, magazine.end(), note[i]) != magazine.end()) // i++; // else{ // cout << "No" <<"\n"; // return; // } // } // cout << "Yes" <<"\n";//Hashing in Strings unordered_map<string, int>words; for(auto &it: magazine){ //N words words[it]++; } for(auto &it : note){// M words if(words[it]> 0){ words[it]--; } else{ cout << "No" <<"\n"; return; } } cout << "Yes" <<"\n"; return;}TC = O(N+M)