针对字母,建立一种映射。
#include <iostream>
using namespace std;
const int MAX = 58;
int isBrotherStr(char* str1, char* str2)
{
if (!str1 && !str2)
{
return -1;
}
else if (!str1 || !str2)
{
return 0;
}
else
{
if (strlen(str1)!=strlen(str2))
{
return 0;
}
int A[MAX]={0};
for (int i = 0; i < strlen(str1); i++)
{
A[str1[i]-'A']++;
A[str2[i]-'A']--;
}
/*
for (int i = 0; i < MAX; i++)
{
cout<<A[i]<<" ";
}
cout<<endl;*/
for (int i = 0; i < MAX; i++)
{
if (A[i]!=0)
{
return 0;
}
return 1;
}
}
}
int main()
{
char str1[] = "abd";
char str2[] = "bda";
if (isBrotherStr(str1, str2) == 1)
{
cout<<str1<<" and "<<str2<<" are brother string."<<endl;
}
else if (isBrotherStr(str1, str2) == 0)
{
cout<<str1<<" and "<<str2<<" are not brother string."<<endl;
}
else
{
cout<<str1<<" and "<<str2<<" are NULL."<<endl;
}
}