对words提前做好处理,在每个queries中去和整个words对比,模拟即可。
class Solution {
public:
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
int n1=queries.size();
int n2=words.size();
char a,b;
int cnt1=0,cnt2=0;
vector<int>ans;
vector<int>c;
int ans1;
for(int i=0;i<n2;i++)
{
cnt2=1;
sort(words[i].begin(),words[i].end());
b=words[i][0];
for(int j=1;;j++)
{
if(words[i][j]==b)
{
cnt2++;
}
else break;
}
c.push_back(cnt2);
}
for(int i=0;i<n1;i++)
{
ans1=0;
cnt1=1;
sort(queries[i].begin(),queries[i].end());
a=queries[i][0];
for(int j=1;;j++)
{
if(queries[i][j]==a)
{
cnt1++;
}
else
{
break;
}
}
for(int j=0;j<n2;j++)
{
if(cnt1<c[j]) ans1++;
}
ans.push_back(ans1);
}
return ans;
}
};