1160. 拼写单词
思路:用哈希表记录
class Solution {
public:
int countCharacters(vector<string>& words, string chars) {
int res=0;
int hash[26];
fill(hash, hash+26, 0);
for(char ch: chars)
hash[ch-'a']++;
for(string word: words){
int tmp[26];
memcpy(tmp, hash, sizeof(hash));//复制hash到tmp中
bool flag=true;
for(char ch: word){
if(tmp[ch-'a']==0) {
flag=false; break;
}
tmp[ch-'a']--;
}
if(flag){
res += word.size();
cout<<word.c_str()<<endl;
}
}
return res;
}
};