# Daily HackerRank Challenge - Day 25

Daily HackerRank Challenge (30 Part Series)

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.

# Hash Tables: Ransom Note Sample Input 0

6 4
give me one grand today night
give one grand today


Sample Output 0

Yes


Sample Input 1

6 5
two times three is not four
two times two is four


Sample Output 1

No


Put each word in magazine into a hash map and traverse note to see if the word occurred in the map or not. If so, deduct the value m[s] by 1 and continue. If m[s] reaches 0, it means there is no enough words to build the random note.

Final Solution

void checkMagazine(vector<string> magazine, vector<string> note) {
unordered_map<string,int> m;
bool f = true;
for(string s:magazine) m[s]++;
for(string s: note) {
if(--m[s]<0) {
f=false;
break;
}
}
cout << (f?"Yes":"No") << "\n";
}


# Count Triplets Sample Input 0

4 2
1 2 2 4


Sample Output 0

2


Create two maps to hold the values for building 2/3 triplets and 3/3 triplets. No division is necessary in this case. Take the sample input 0 as an exampe, for the first value, ans and m3[v*r] is 0 as m3 and m2 are still empty. For the first iternation, it only set m2[1*2] to 1.

For the second iteration, the value is 2 and m3 is still empty. However m2 is 1 as it is updated in previous iteration so that m3 is now 1.

For the third iteration, the value is 2 again and m3 is still empty. m3 is updated again, which is 2.

For the last iternation, m3 is 2 and ans becomes 2. At the end, it returns 2.

i=0 - value=1
m2=1

i=1 - value=2
m3=1
m2=1

i=2 - value=2
m3=2
m2=2

i=3 - value=4
ans=m3
ans=2


Final Solution

long countTriplets(vector<long> arr, long r) {
unordered_map<long,long> m2,m3;
int ans=0;
for(long v : arr){
ans += m3[v];
m3[v*r] += m2[v];
++m2[v*r];
}
return ans;
}


# Complete Code

Check out the complete code via below links

Daily HackerRank Challenge (30 Part Series)

### Discussion   