-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (32 loc) · 996 Bytes
/
Copy pathSolution.java
File metadata and controls
39 lines (32 loc) · 996 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
33
34
35
36
37
38
39
package org.example.problems.ransom_note;
import org.example.helpers.CharsCountMap;
import org.example.problems.SolutionInterface;
public class Solution implements SolutionInterface {
@Override
public String getName() {
return "Ransom Note";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/ransom-note/";
}
public boolean canConstruct(String ransomNote, String magazine) {
CharsCountMap map = new CharsCountMap();
int letters = 0;
for (char c: ransomNote.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
letters++;
}
for (char c: magazine.toCharArray()) {
int current = map.getOrDefault(c, 0);
if (current > 0) {
map.put(c, current - 1);
letters--;
if (letters == 0) {
return true;
}
}
}
return false;
}
}