题目描述
如果两个字符串中的字符一样,出现次数也一样,只是出现的顺序不一样,则认为这两个字符串是兄弟字符串。如何判断两个字符串是兄弟字符串。
代码解析:
#include <iostream>
#include <string>
using namespace std;
bool IsBrother(string a, string b) {
if (a.size() != b.size()) {
return false;
}
int hashTable1[26] = { 0 };
int hashTable2[26] = { 0 };
int n = a.size();
for (int i = 0; i < n; ++i) {
hashTable1[a[i] - 'a']++;
hashTable2[b[i] - 'a']++;
}
for (int j = 0; j < 26; ++j) {
if (hashTable1[j] != hashTable2[j]) {
return false;
}
}
return true;
}
int main() {
string s1, s2;
while (cin >> s1 >> s2) {
if (IsBrother(s1, s2)) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}
return 0;
}