-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathransomNote.java
More file actions
40 lines (33 loc) · 1.18 KB
/
ransomNote.java
File metadata and controls
40 lines (33 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//HACKERRANK Ransom Note
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class ransomNote {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>(Arrays.asList("give", "me", "one", "grand", "today", "night"));
List<String> list2 = new ArrayList<>(Arrays.asList("give", "one", "grand", "today"));
checkMagazine(list1, list2);
}
public static void checkMagazine(List<String> magazine, List<String> note){
HashMap<String, Integer> hm = new HashMap<>();
for(int i = 0; i < note.size(); i++){
hm.put(note.get(i), hm.getOrDefault(note.get(i), 0) + 1);
}
for(int j = 0; j < magazine.size(); j++){
hm.put(magazine.get(j), hm.getOrDefault(magazine.get(j), 0) - 1);
}
boolean ransomSuccess = true;
for(Map.Entry<String, Integer> entry : hm.entrySet()){
if(entry.getValue() > 0){
ransomSuccess = false;
}
}
if(ransomSuccess){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}