Cracking the Interview - hashmap

The Using of HashMap

A kidnapper wrote a ransom note but is worried it will be traced back to him. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use whole words available in the magazine, meaning he cannot use substrings or concatenation to create the words he needs.

Here is my solution to this problem in Java
https://www.hackerrank.com/challenges/ctci-ransom-note

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {   
    public static void fillmap(HashMap<String, Integer> map, String[] value ){
        if(value == null) return;
        for(int i = 0; i < value.length; i++){
            if(!map.containsKey(value[i]))
                map.put(value[i],1);
            else {
               Integer current = map.get(value[i]);
                if (current == null) current = 0;
                map.put(value[i], current+1);
            }
        }
    }    
    public static String Answer(String[] magazine, String[] ransom){
        HashMap<String, Integer> mapA = new HashMap<String, Integer>();
        HashMap<String, Integer> mapB = new HashMap<String, Integer>();
        if(magazine.length < ransom.length)
            return "No";  
        //input magazine into hashmap
        fillmap(mapA, magazine);
        // input ransom into hashmap
       fillmap(mapB, ransom);
        for(String str: mapB.keySet()){
            if(!mapA.containsKey(str))
                return "No";
            Integer countA = mapA.get(str);
            Integer countB = mapB.get(str);
            if(countB > countA)
                return "No";

        }
        return "Yes";
    }
    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();
        }
        System.out.println(Answer(magazine, ransom));
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,262評(píng)論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,682評(píng)論 5 6
  • 我想回到那個(gè),即使有煩惱,也能呼呼入眠的時(shí)候。
    一嵐閱讀 223評(píng)論 0 0
  • 01 杜若夢(mèng)見(jiàn)被程子航表白了,夢(mèng)里她傻傻地笑著說(shuō):“我愿意啊,子航,我愿意永遠(yuǎn)和你在一起”說(shuō)完轉(zhuǎn)身又睡著了,經(jīng)常說(shuō)...
    碎碎念小乖閱讀 1,155評(píng)論 5 46
  • DevOps 是一個(gè)席卷 IT 界的新術(shù)語(yǔ)。但它究竟是什么,南非的公司們?nèi)绾卫盟鼇?lái)加快高品質(zhì)應(yīng)用程序的開(kāi)發(fā)速度?...
    OneAPM閱讀 382評(píng)論 0 1

友情鏈接更多精彩內(nèi)容