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++;
}