Skip to content

Commit e38efec

Browse files
authored
Create Find Words That Can Be Formed by Characters
1 parent 43500f7 commit e38efec

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int countCharacters(String[] words, String chars) {
3+
HashMap<Character, Integer> table1 = new HashMap<>();
4+
int sum = 0;
5+
for(int i = 0; i<chars.length(); i++){
6+
table1.put(chars.charAt(i), table1.getOrDefault(chars.charAt(i), 0)+1);
7+
}
8+
for(int i = 0; i<words.length; i++){
9+
boolean has = true;
10+
HashMap<Character, Integer> table = (HashMap<Character, Integer>) table1.clone();
11+
for(int j = 0; j<words[i].length(); j++){
12+
if(!table.containsKey(words[i].charAt(j))){
13+
has = false;
14+
break;
15+
}
16+
table.put(words[i].charAt(j), table.get(words[i].charAt(j))-1);
17+
if(table.get(words[i].charAt(j)) == 0){
18+
table.remove(words[i].charAt(j));
19+
}
20+
}
21+
if(has){
22+
sum += words[i].length();
23+
}
24+
}
25+
return sum;
26+
}
27+
}

0 commit comments

Comments
 (0)