-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode1160_findWords.java
42 lines (32 loc) · 1.1 KB
/
leetcode1160_findWords.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
37
38
39
40
41
42
import java.util.ArrayList;
import java.util.List;
public class leetcode1160_findWords {
public static int countCharacters(String[] words, String chars) {
boolean goodString = true;
int sumGoodStrings = 0;
List<Character> list = new ArrayList<>();
for (String word : words) {
goodString = true;
list.clear();
for (int k = 0; k < chars.length(); k++) {
list.add(chars.charAt(k));
}
for (int i = 0; i < word.length(); i++) {
if (list.contains(word.charAt(i))) {
list.remove((Character) word.charAt(i));
} else {
goodString = false;
}
}
if (goodString)
sumGoodStrings = sumGoodStrings + word.length();
}
return sumGoodStrings;
}
public static void main(String[] args) {
String[] str = {"hello", "world", "leetcode"};
String chars = "welldonehoneyr";
int num = countCharacters(str, chars);
System.out.println(num);
}
}