0%

面试题 01.02. 判定是否互为字符重排

面试题 01.02. 判定是否互为字符重排

3次遍历O(3n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
int a[26]{ 0 };
for(auto& ch : s1)
a[ch - 'a'] += 1;
for(auto& ch : s2)
a[ch - 'a'] -= 1;
for(int i = 0; i < 26; ++i)
if(a[i] != 0)
return false;
return true;
}
};