-
Notifications
You must be signed in to change notification settings - Fork 864
/
HashTablesRansomNote.java
58 lines (52 loc) · 1.52 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
50
51
52
53
54
55
56
57
58
/**
*
* Problem Statement-
* [Hash Tables: Ransom Note](https://www.hackerrank.com/challenges/ctci-ransom-note/problem)
*
*/
package com.javaaid.hackerrank.solutions.tutorials.ctci;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class HashTablesRansomNote {
public static boolean isMessageReplicable(String[] magazine, String[] ransom) {
Map<String, Integer> magazineWords = new HashMap<String, Integer>();
for (int i = 0; i < magazine.length; i++) {
Integer wordCount = magazineWords.get(magazine[i]);
if (wordCount == null) {
magazineWords.put(magazine[i], 1);
} else {
magazineWords.put(magazine[i], wordCount + 1);
}
}
for (int i = 0; i < ransom.length; i++) {
Integer wordCount = magazineWords.get(ransom[i]);
if (wordCount == null || wordCount == 0) {
return false;
} else {
magazineWords.put(ransom[i], wordCount - 1);
}
}
return true;
}
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 result = isMessageReplicable(magazine, ransom) ? "Yes" : "No";
System.out.println(result);
in.close();
}
}