-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRansomNoteChecker.java
More file actions
33 lines (22 loc) · 896 Bytes
/
Copy pathRansomNoteChecker.java
File metadata and controls
33 lines (22 loc) · 896 Bytes
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
package LeetcodePractice;
import java.util.HashMap;
public class RansomNoteChecker {
public static boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> magazineCount = new HashMap<>();
for (char ch : magazine.toCharArray()) {
magazineCount.put(ch, magazineCount.getOrDefault(ch, 0) + 1);
}
for (char ch : ransomNote.toCharArray()) {
if (!magazineCount.containsKey(ch) || magazineCount.get(ch) == 0) {
return false;
}
magazineCount.put(ch, magazineCount.get(ch) - 1);
}
return true;
}
public static void main(String[] args) {
System.out.println(canConstruct("a", "b"));
System.out.println(canConstruct("aa", "ab"));
System.out.println(canConstruct("aa", "aab"));
}
}