- 封装函数:
private:
int counter(vector<int>chars_cnt, vector<int>word_cnt, string word)
{
int tempres = 0;
for(int i=0; i<26; i++)
{
if(chars_cnt[i]<word_cnt[i])
{
return 0;
}
}
tempres += word.size();
return tempres;
}
public:
int countCharacters(vector<string>& words, string chars){
vector<int> chars_cnt(26,0);
int res = 0;
for(auto c :chars)
{
chars_cnt[c-'a']++;
}
for(auto word : words)
{
vector<int> word_cnt(26,0);
for(auto c : word)
{
word_cnt[c-'a']++;
}
res += counter(chars_cnt, word_cnt, word);
}
return res;
}
- flag
- public:
int countCharacters(vector& words, string chars){
int chars_cnt[26] = {0};
int res = 0;
for(auto c :chars)
{
chars_cnt[c-‘a’]++;
}
for(auto word : words)
{
int word_cnt[26] = {0};
for(auto c : word)
{
word_cnt[c-‘a’]++;
}
bool canspell = true;
for(int i=0; i<26; i++)
{
if(chars_cnt[i]<word_cnt[i])
{
canspell = false;
break;
}
}
if(canspell)
{
res += word.size();
}
}
return res;
}