一、问题描述

二、解题思路
解法一:
对s1和s2进行sort排序,返回s1是否等于s2;
解法二:
用哈希表分别来记录s1和s2中字符出现的次数,统计完后,判断两个哈希表是否相等;
三、代码实现
解法一:
时间复杂度:T(n)=O(nlogn)
空间复杂度:S(n)=O(1)
cpp
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
return s1==s2;
}
};
解法二:
时间复杂度:T(n)=O(n)
空间复杂度:S(n)=O(n)
cpp
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
unordered_map<char,int> hash1,hash2;
for(auto x:s1) hash1[x]++;
for(auto x:s2) hash2[x]++;
return hash1==hash2;
}
};