1. 题目解析
本题判断的是两个字符串,其中一个字符串变换顺序是否可以变化为第二个字符串,即重排后是否为同一字符串
2. 算法原理
这里我的思路是直接统计两字符串中字符出现的次数,然后直接比较即可,我使用了范围for以及字符ASCII码值充当下标来创建两个数组存储两个字符串中字符出现的次数,最后比较即可
3. 代码实现
class Solution {
public:
bool CheckPermutation(string s1, string s2)
{
int count1[26] = {0};
int count2[26] = {0};
for(auto ch : s1)
{
count1[ch - 'a']++;
}
for(auto ch : s2)
{
count2[ch - 'a']++;
}
for(int i = 0;i < 26;i++)
{
if(count1[i]!=count2[i])
{
return false;
}
}
return true;
}
};