-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
30 lines (26 loc) · 914 Bytes
/
main.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
class Solution {
public int countCharacters(String[] words, String chars) {
Map<Character, Integer> counts = new HashMap();
for (Character c : chars.toCharArray()) {
counts.put(c, counts.getOrDefault(c, 0) + 1);
}
int ans = 0;
for (String word : words) {
Map<Character, Integer> wordCount = new HashMap();
for (Character c : word.toCharArray()) {
wordCount.put(c, wordCount.getOrDefault(c, 0) + 1);
}
boolean good = true;
for (Character c : wordCount.keySet()) {
if (counts.getOrDefault(c, 0) < wordCount.get(c)) {
good = false;
break;
}
}
if (good) {
ans += word.length();
}
}
return ans;
}
}