-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRansomNote.java
28 lines (25 loc) · 955 Bytes
/
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
import java.util.HashMap;
public class RansomNote {
public static void main(String[] args) {
String ransomNote = "a";
String magazine = "b";
boolean result = checkPossibility(ransomNote, magazine);
System.out.println(result);
}
public static boolean checkPossibility(String ransomNote, String magazine) {
HashMap<Character, Integer> mag = new HashMap<>();
HashMap<Character, Integer> ran = new HashMap<>();
for (int i = 0; i < magazine.length(); i++) {
mag.put(magazine.charAt(i), mag.getOrDefault(magazine.charAt(i), 0) + 1);
}
for (int i = 0; i < ransomNote.length(); i++) {
ran.put(ransomNote.charAt(i), ran.getOrDefault(ransomNote.charAt(i), 0) + 1);
}
for (char ch : ran.keySet()) {
if (ran.get(ch) > mag.getOrDefault(ch, 0)) {
return false;
}
}
return true;
}
}