-
Notifications
You must be signed in to change notification settings - Fork 5
/
HashTablesRansomNote.java
49 lines (42 loc) · 1.53 KB
/
HashTablesRansomNote.java
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
41
42
43
44
45
46
47
48
49
// https://www.hackerrank.com/challenges/ctci-ransom-note/problem
package dictionaries;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class HashTablesRansomNote {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int magazineWords = scanner.nextInt();
int ransomWords = scanner.nextInt();
List<String> magazine = getWords(magazineWords);
List<String> ransomNote = getWords(ransomWords);
System.out.println(canUseMagazine(magazine, ransomNote) ? "Yes" : "No");
}
private static List<String> getWords(int length) {
List<String> words = new ArrayList<>();
while (length-- > 0) {
words.add(scanner.next());
}
return words;
}
private static boolean canUseMagazine(List<String> magazine, List<String> ransom) {
Map<String, Integer> frequency = getFrequency(magazine);
for (String word : ransom) {
if (frequency.containsKey(word) && frequency.get(word) > 0) {
frequency.put(word, frequency.get(word) - 1);
} else {
return false;
}
}
return true;
}
private static Map<String, Integer> getFrequency(List<String> words) {
Map<String, Integer> result = new HashMap<>();
for (String word : words) {
result.put(word, result.getOrDefault(word, 0) + 1);
}
return result;
}
}