This is my solution to challenge “Hash Tables: Ransom Note” on HackerRank. Click here to see the challenge.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        String magazine[] = new String[m];
        for(int magazine_i=0; magazine_i < m; magazine_i++){
            magazine[magazine_i] = in.next();
        }
        String ransom[] = new String[n];
        for(int ransom_i=0; ransom_i < n; ransom_i++){
            ransom[ransom_i] = in.next();
        }
        String output = isRansomPossible(magazine, ransom)?"Yes":"No";
        System.out.println(output);
    }
    public static boolean isRansomPossible(String[] magazine, String[] ransom){
        if(ransom.length > magazine.length){
            return false;
        }
        HashMap<String,Integer> wordMap = new HashMap<String,Integer>();
        for(String word:magazine){
            if(wordMap.containsKey(word)){
                int n = wordMap.get(word);
                wordMap.put(word,++n);
            }else{
                wordMap.put(word,1);
            }
        }
        for(String word:ransom){
            if(wordMap.containsKey(word) && wordMap.get(word) > 0){
                int n = wordMap.get(word);
                n--;
                wordMap.put(word,n);
            }else{
                return false;
            }
        }
        return true;
    }
}