看了别人的思路自己写的,思路懂了就不难
class Solution {
public:
bool closeStrings(string word1, string word2) {
if(word1.size() != word2.size())
return false; //比较字符串长度
int fre1[26], fre2[26], len=word1.size();
memset(fre1,0,sizeof(fre1));
memset(fre2,0,sizeof(fre2));
vector<char> set1,set2;
vector<int> fre11, fre22;
for(int i=0;i<len;++i){
fre1[word1[i]-'a']++;
if(fre1[word1[i]-'a']==1)
set1.push_back(word1[i]);
fre2[word2[i]-'a']++;
if(fre2[word2[i]-'a']==1)
set2.push_back(word2[i]);
}
sort(set1.begin(), set1.end());
sort(set2.begin(), set2.end());
if(set1!=set2)
return false; //比较字符集合是否一样
for(int i=0;i<26;++i){
if(fre1[i]!=0) fre11.push_back(fre1[i]);
if(fre2[i]!=0) fre22.push_back(fre2[i]);
}
sort(fre11.begin(), fre11.end());
sort(fre22.begin(), fre22.end());
return fre11==fre22; //比较字符频率是否一样
}
};