您的位置:首页 > 其它

cracking the coding interview No1.3

2014-10-19 15:26 344 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: