方法还是数字计数
#include <iostream>
#include<string>
using namespace std;
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
int charCount1[100] = { 0 };//全用0进行初始化
int charCount2[100] = { 0 };//全用0进行初始化
if (s1.length() != s2.length())
return false;
for(int i = 0; i < int(s1.length()); i++){
charCount1[s1[i] - 'a']++;
charCount2[s2[i] - 'a']++;
}
for (int i = 0; i < 100; i++){//这里这个100用成了s1的长度,下次对象与操作都要写清楚
if (abs(charCount1[i] - charCount2[i])>0){
return false;
}
}
return true;
}
};
void main(){
string a = "cuhv"; //C++中严格很多,双引号表示字符串、单引号表示字符
string c = "cuvs";
Solution object_f;
bool b = object_f.CheckPermutation(a,c);
cout << b << endl;
}
这道题他们有用位运算、hash表、排序来做的。