class Solution {
public:
bool isAnagram(string s, string t) {
std::sort(s.begin(),s.end());
std::sort(t.begin(),t.end());
if (s==t)
{
return true;
}
else
{
return false;
}
if(s.size()!=t.size())
return false;
else{
unordered_map<char,int>ss;
unordered_map<char,int>tt;
for(int i=0;i<s.size();i++)
{
ss[s[i]]++;
tt[t[i]]++;
}
if(ss==tt)
{
return true;
}
else
return false;
}
// self_hash_table_method
if(s.size()!=t.size())
return false;
else
{
int a[26]={0};
for (int i=0;i<s.size();i++)
{
a[s[i]-'a']++;
}
for(int i=0;i<t.size();i++)
{
--a[t[i]-'a'];
if(a[t[i]-'a']<0)
return false;
}
return true;
}
}
};
这三种方法
第一种是排序的方法
第二种是用了c++的标准哈希容器来做的
第三种是用线性数组来表示哈希表的结果
leetcode跑出来的时间结果是
第三种<第二种<第一种
但我个人在vs下面跑出来的时间是有时候第二种方法用时较多,