Skip to content

Commit 3cd5e6a

Browse files
committed
RansomNote: done
1 parent cc348a9 commit 3cd5e6a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.hackerrank.tutorials.ctci;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Scanner;
6+
7+
/**
8+
* Question: https://www.hackerrank.com/challenges/ctci-ransom-note
9+
*
10+
* @author ramswaroop
11+
* @version 30/09/2016
12+
*/
13+
public class RansomNote {
14+
15+
Map<String, Integer> magazineMap;
16+
Map<String, Integer> noteMap;
17+
18+
public RansomNote(String magazine, String note) {
19+
20+
magazineMap = new HashMap<>();
21+
noteMap = new HashMap<>();
22+
String[] magazineWords = magazine.split(" ");
23+
String[] noteWords = note.split(" ");
24+
Integer c;
25+
26+
for (int i = 0; i < magazineWords.length; i++) {
27+
if ((c = magazineMap.get(magazineWords[i])) == null) {
28+
magazineMap.put(magazineWords[i], 1);
29+
} else {
30+
magazineMap.put(magazineWords[i], c + 1);
31+
}
32+
}
33+
34+
for (int i = 0; i < noteWords.length; i++) {
35+
if ((c = noteMap.get(noteWords[i])) == null) {
36+
noteMap.put(noteWords[i], 1);
37+
} else {
38+
noteMap.put(noteWords[i], c + 1);
39+
}
40+
}
41+
}
42+
43+
public boolean solve() {
44+
for (Map.Entry<String, Integer> entry : noteMap.entrySet()) {
45+
if (magazineMap.get(entry.getKey()) == null || magazineMap.get(entry.getKey()) - entry.getValue() < 0) {
46+
return false;
47+
}
48+
}
49+
return true;
50+
}
51+
52+
public static void main(String[] args) {
53+
Scanner scanner = new Scanner(System.in);
54+
int m = scanner.nextInt();
55+
int n = scanner.nextInt();
56+
57+
// Eat whitespace to beginning of next line
58+
scanner.nextLine();
59+
60+
RansomNote s = new RansomNote(scanner.nextLine(), scanner.nextLine());
61+
scanner.close();
62+
63+
if (s.solve()) {
64+
System.out.println("Yes");
65+
} else {
66+
System.out.println("No");
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)