方法一:转成数字,然后把基准转换成计数数组(和前面的桶排序类似方法类型),然后每一次遍历单词的时候,都要用计数数组去匹配,看看是否能匹配上,匹配上就把单词中的char数量加到总sum中
public static int countCharacters(String[] words, String chars) {
int count = 0;
int[] jizhun = new int[26];
for (char c : chars.toCharArray()){
jizhun[c - 'a']++;
}
for (String word : words) {
int[] tjizhun = jizhun.clone();
int Tcount = 0;
for (char c : word.toCharArray()) {
if (tjizhun[c - 'a'] > 0) {
jizhun[c - 'a']--;
Tcount++;
}
}
if (Tcount == word.length())
count += Tcount;
}
return count;
}
方法2:可以用map但是今天晚上有点困,有时间再去做