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.
publicstaticvoidcheckMagazine(List<String>magazine,List<String>note){booleancanBePerformed=false;//create hashmap key: word && value: occurrence of the wordHashMap<String,Integer>magazineMap=newHashMap<>();//fill the magazineMapfor(StringmagazineWord:magazine){magazineMap.put(magazineWord,Collections.frequency(magazine,magazineWord));}//loop in list notefor(StringnoteWord:note){if(!magazineMap.containsKey(noteWord)||magazineMap.get(noteWord)==0){canBePerformed=false;break;}elsemagazineMap.put(noteWord,magazineMap.get(noteWord)-1);canBePerformed=true;}if(canBePerformed==true){System.out.println("Yes");}else{System.out.println("No");}}
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 →