您的位置:首页 > 其它

CareerCup 1.3

2013-03-21 13:28 274 查看
1.3 Given two strings, write a method to decide if one is a permutation of the other.

Solution 1:

bool isPermutation(string a, string b) {
if (a.length() != b.length())
return false;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
return a == b;
}

Solution 2:

bool isPermutation(string a, string b) {
if (a.length() != b.length())
return false;

vector<int> c(256, 0);
for (int i = 0; i < a.length(); i++)
c[a[i]]++;
for (int i = 0; i < b.length(); i++) {
c[b[i]]--;
if (c[b[i]] < 0)
return false;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: