题目链接
思路
用两个数组分别取统计两个字符串中各个字符出现的次数,进而进行判断
实现
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
string shop,need;
int countS[123]={0},countN[123]={0};
cin>>shop>>need;
int lenS=strlen(shop.c_str());
int lenN=strlen(need.c_str());
bool allExist=true;
int cha=0;
for(int i=0;i<lenS;i++)
{
countS[(int)shop[i]]+=1;
}
for(int j=0;j<lenN;j++)
{
countN[(int)need[j]]+=1;
}
for(int m=0;m<123;m++)
{
if(countN[m]!=0)
{
if(countN[m]>countS[m])
{
cha+=(countN[m]-countS[m]);
allExist=false;
}
}
}
if(allExist)
{
cout<<"Yes "<<lenS-lenN;
}
else
{
cout<<"No "<<cha;
}
return 0;
}