-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution.java
68 lines (52 loc) · 1.97 KB
/
Solution.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
/*
* DeveloperName(): Jignesh Chudasama
* GithubName(): https://github.com/Jignesh-81726
*/
import java.util.*;
public class Solution {
Map<String, Integer> magazineMap;
Map<String, Integer> noteMap;
public Solution(String magazine, String note) {
magazineMap = new HashMap<String, Integer>();
noteMap = new HashMap<String, Integer>();
fillMap(magazineMap, magazine);
fillMap(noteMap, note);
}
public void fillMap(Map<String, Integer> map, String value) {
if (value == null) return;
String[] words = value.split("\\s+");
for (String word : words) {
if (!map.containsKey(word)) {
map.put(word, 1);
} else {
Integer current = map.get(word);
if (current == null) current = 0;
map.put(word, current + 1);
}
}
}
public boolean solve() {
for (String key : noteMap.keySet()) {
// If magazine doesn't have a word that's on a note, then return false right the way
if (!magazineMap.containsKey(key)) return false;
// Now make sure there are enough words on magazine for a given word
Integer magazineCount = magazineMap.get(key);
Integer noteCount = noteMap.get(key);
if (magazineCount < noteCount) 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();
Solution s = new Solution(scanner.nextLine(), scanner.nextLine());
scanner.close();
boolean answer = s.solve();
if(answer)
System.out.println("Yes");
else System.out.println("No");
}
}