64,649
社区成员
发帖
与我相关
我的任务
分享
#include <map>
#include <iostream>
#include <string.h>
using namespace std;
bool is_matching( const char* word1, const char* word2);
int main(int argc, char **argv)
{
if( is_matching("abab","bbaa") )
{
cout << "==\n";
}
else
{
cout << "!=\n";
}
return 0;
}
bool is_matching(const char* word1, const char* word2)
{
int len;
if( NULL == word1 || NULL == word2 || (len = strlen(word1)) != strlen(word2) )
{
return false;
}
map<char, int> m1,m2;
for( int i=0; i<len; i++)
{
m1[word1[i]]++;
m2[word2[i]]++;
}
return m1 == m2;
}
#include <iostream>
#include <map>
#include <string>
using namespace std;
bool is_matching( const char* word1, const char* word2)
{
int a[26]={0},b[26]={0};
if(strlen(word1)!=strlen(word2))
return false;
for(int i=0;i<strlen(word1);i++)
{
a[*(word1+i)-'a']++;
b[*(word2+i)-'a']++;
}
for(int i=0;i<26;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
int main()
{
char *w1="hell",*w2="llohe";
if(is_matching(w1,w2))
cout<<w1<<" matches "<<w2;
else
cout<<w1<<" does not match "<<w2;
return 0;
}
bool is_matching( const char* word1, const char* word2)
{
int bit_map[26]={0}; //只针对小写字母,所以是26个,包括大写的话,52个元素即可
int i;
if(strlen(word1)!=strlen(word2) )
return false;
for(i=0; i<strlen(word1); ++i)
{
bit_map[(word1[i]-'a')]++;
bit_map[(word2[i]-'a')]--;
}
for(i=0; i<26; ++i)
if(bit_map[i]!=0)
return false;
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
char a[]="aabbc";
char b[]="abcab";
if(is_matching( a, b))
cout<<"match"<<endl;
else
cout<<"false"<<endl;
system("pause");
return 0;
}