#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1="RABDCLMORS";
string str2="ZRSD";
//开辟一个辅助数组并清零
int hash[26]={0};
int num = 0;//数组中元素个数
//扫描短字符串
for(int j = 0;j<str2.length();j++)
{
int index = str2[j]-'A';
if(hash[index] == 0)
{
hash[index] = 1;
num++;
}
}
//扫描长字符串
for(int k=0;k<str1.length();k++)
{
int index = str1[k]-'A';
//如果数组元素索引为1,则清零
if(hash[index]==1)
{
hash[index] = 0;
num--;
if(num == 0)
break;
}
}
if(num == 0)
cout<<"true"<<endl;
else
cout<<"false"<<endl;
return 0;
}
两个字符串包含hashtalbe的O(m+n)
最新推荐文章于 2024-06-24 08:55:24 发布