64,642
社区成员
发帖
与我相关
我的任务
分享
bool func(char word1[], char word2[])
{
char tmp, *p, *p1 = word1, *p2 = word2;
if (strlen(word1) != strlen(word2))
return false;
while (tmp = *word1)
{
p = word1;
while (p > p1)
{
if (*p < *(p - 1))
*p = *(p - 1);
else
break;
--p;
}
*p = tmp;
++word1;
}
while (tmp = *word2)
{
p = word2;
while (p > p2)
{
if (*p < *(p - 1))
*p = *(p - 1);
else
break;
--p;
}
*p = tmp;
++word2;
}
while (*p1)
{
if (*p1 != *p2)
return false;
++p1;
++p2;
}
return true;
}
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
bool is_anagram (std::string x, std::string y)
{
std::sort(x.begin(), x.end());
std::sort(y.begin(), y.end());
return x == y;
}
int main (int, char**)
{
cout << is_anagram("silent", "listen") << endl; // 1
cout << is_anagram("silent", "lesson") << endl; // 0
return 0;
}
bool func(const char *word1, const char *word2)
{
char alpha1[26] = {0};
char alpha2[26] = {0};
int i = 0;
while (*word1)
{
alpha1[(*word1 | 0x20) - 'a']++;
word1++;
}
while (*word2)
{
alpha2[(*word2 | 0x20) - 'a']++;
word2++;
}
for (; i < 26; i++)
{
if (alpha1[i] != alpha2[i])
return false;
}
return true;
}