代码实现(C++):
class Solution {
public:
int countCharacters(vector<string>& words, string chars) {
unordered_map<char,int> chars_cnt;
int cnt = 0;
for(char c : chars)
++chars_cnt[c];
for(string word : words){
unordered_map<char,int> word_cnt;
for(char c : word)
++word_cnt[c];
bool is_cnt = true;
for(char c : word){
if(word_cnt[c] > chars_cnt[c]){
is_cnt = false;
break;
}
}
if(is_cnt)
cnt += word.size();
}
return cnt;
}
};