1160. Find Words That Can Be Formed by Characters
class Solution {
private int[] count = new int[26];
public int countCharacters(String[] words, String chars) {
for (char c : chars.toCharArray()) {
count[c - 'a']++;
}
int length = 0;
for (String word : words) {
if (canBeFormed(word)) {
length += word.length();
}
}
return length;
}
private boolean canBeFormed(String word) {
int[] localCount = new int[26];
for (char c : word.toCharArray()) {
localCount[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (localCount[i] > count[i]) {
return false;
}
}
return true;
}
}