1. 题目


2. 解题思路
计数即可
3. 代码
class Solution {
public:
int countCharacters(vector<string>& words, string chars) {
unordered_map<char, int> chars_map;
for(char s : chars){
chars_map[s]++;
}
int ans = 0;
see:for(string word : words){
unordered_map<char, int> chars_word_map;
for(char s : word){
chars_word_map[s]++;
}
int add = word.size();
for(auto wc : chars_word_map){
if(wc.second > chars_map[wc.first]){
add = 0;
}
}
ans += add;
}
return ans;
}
};
4. c++ map的遍历方式
for(map<string,string>::iterator it = map.begin(); it!=map.end(); it++)
cout<<it->first<<"\t";
cout<<it->second<<endl;
it++;
}

本文介绍了一种使用C++实现的字符计数算法,通过遍历字符串并利用unordered_map进行字符频率统计,实现了对一系列单词中可用字符的高效计数。文章详细展示了算法的实现过程,包括初始化字符映射、遍历单词列表以及比较字符频率等关键步骤。
917

被折叠的 条评论
为什么被折叠?



