forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRansomNote.java
70 lines (58 loc) · 1.88 KB
/
RansomNote.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
59
60
61
62
63
64
65
66
67
68
69
70
package com.hackerrank.tutorials.ctci;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Question: https://www.hackerrank.com/challenges/ctci-ransom-note
* Level: Easy
*
* @author rampatra
* @version 30/09/2016
*/
public class RansomNote {
Map<String, Integer> magazineMap;
Map<String, Integer> noteMap;
public RansomNote(String magazine, String note) {
magazineMap = new HashMap<>();
noteMap = new HashMap<>();
String[] magazineWords = magazine.split(" ");
String[] noteWords = note.split(" ");
Integer c;
for (int i = 0; i < magazineWords.length; i++) {
if ((c = magazineMap.get(magazineWords[i])) == null) {
magazineMap.put(magazineWords[i], 1);
} else {
magazineMap.put(magazineWords[i], c + 1);
}
}
for (int i = 0; i < noteWords.length; i++) {
if ((c = noteMap.get(noteWords[i])) == null) {
noteMap.put(noteWords[i], 1);
} else {
noteMap.put(noteWords[i], c + 1);
}
}
}
public boolean solve() {
for (Map.Entry<String, Integer> entry : noteMap.entrySet()) {
if (magazineMap.get(entry.getKey()) == null || magazineMap.get(entry.getKey()) - entry.getValue() < 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
// Eat whitespace to beginning of next line
scanner.nextLine();
RansomNote s = new RansomNote(scanner.nextLine(), scanner.nextLine());
scanner.close();
if (s.solve()) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}