- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1160.java
36 lines (33 loc) · 1.11 KB
/
_1160.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.Map;
publicclass_1160 {
publicstaticclassSolution1 {
publicintcountCharacters(String[] words, Stringchars) {
intlength = 0;
Map<Character, Integer> map = newHashMap<>();
for (charc : chars.toCharArray()) {
intcount = map.getOrDefault(c, 0);
map.put(c, count + 1);
}
for (Stringword : words) {
if (canForm(word, map)) {
length += word.length();
}
}
returnlength;
}
privatebooleancanForm(Stringword, finalMap<Character, Integer> map) {
Map<Character, Integer> tmp = newHashMap<>(map);
for (Characterc : word.toCharArray()) {
if (tmp.containsKey(c) && tmp.get(c) > 0) {
intcount = tmp.get(c);
tmp.put(c, count - 1);
} else {
returnfalse;
}
}
returntrue;
}
}
}