-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode1160.java
More file actions
37 lines (34 loc) · 1.1 KB
/
Copy pathleetcode1160.java
File metadata and controls
37 lines (34 loc) · 1.1 KB
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
package leetcode;
import java.util.HashMap;
import java.util.Map;
public class leetcode1160 {
public int countCharacters(String[] words, String chars) {
int res=0;
Map<Character,Integer> own=new HashMap<>();
for (Character c:
chars.toCharArray()) {
own.put(c,own.getOrDefault(c,0)+1);
}
for (int i = 0; i <words.length ; i++) {
char [] temp=words[i].toCharArray();
Map<Character,Integer> tempmap=new HashMap<>();
boolean ismatch=true;
for (char c:
temp) {
tempmap.put(c,tempmap.getOrDefault(c,0)+1);
if (own.getOrDefault(c,0)==0||tempmap.get(c)>own.get(c)){ //字符表么没有或者少于
ismatch=false;
}
}
if (ismatch){
res+=words[i].length();
}
}
return res;
}
public static void main(String[] args) {
new leetcode1160().countCharacters(new String[]{
"hello","world","leetcode"
},"welldonehoneyr");
}
}