方法1
先排序 后判断是否相等
#include "stdafx.h"
#include <algorithm>
#include <string>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
return s == t;
}
};
方法二直接统计各个字符出现的顺序
#include <vector>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> v(26, 0);
for (int i = 0; i < s.size(); i++)
{
v[s[i] - 'a']++;
}
for (int i = 0; i < t.size(); i++)
{
v[t[i] - 'a']--;
}
for (int i = 0; i < 26; i++)
{
if (v[i] != 0)
return false;
}
return true;
}
};