1.3 Given two strings,write a method to decide if one is permutation of the other.
//(1)O(nlogn)排序后比较字符串
//(2)O(n)hash table
bool isAnagram (string s1, string s2)
{
if (s1 == NULL || s2 == NULL)
{
return false;
}
if (s1.length() != s2.length)
{
return false;
}
int length = s1.length();
int hash[256];
memset(hash,0,sizeof(hash));
for (int i = 0; i < length; i++)
{
hash[(int)s1[i]]++;
hash[(int)s2[i]]--;
}
for (int i = 0; i < 256; i++)
{
if (hash[i])
{
return false;
}
}
return true;
}