LeetCode 1160. 拼写单词
滑动窗口
class Solution {
public:
map<char,int> mp;
bool judge(map<char,int> mp1)
{
map<char,int>::iterator it=mp1.begin();
bool flag=true;
for(;it!=mp1.end();it++)
{
if(it->second>mp[it->first])
{
flag=false;
}
}
return flag;
}
int countCharacters(vector<string>& words, string chars) {
for(int i=0;i<chars.size();i++)
{
mp[chars[i]]++;
}
int sum=0;
for(int i=0;i<words.size();i++)
{
map<char,int> mp1;
for(int j=0;j<words[i].size();j++)
{
char c=words[i][j];
mp1[c]++;
}
if(judge(mp1))
{
sum+=words[i].size();
}
}
return sum;
}
};