leetcode 242: valid anagram

Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.

Note:
You may assume the string contains only lowercase alphabets.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int (const char *a, const char *b) {
return *a - *b;
}
bool isAnagram(char* s, char* t) {
int lenS = strlen(s);
int lenT = strlen(t);
if (lenS != lenT) {
return false;
}
qsort(s, lenS, sizeof(char), cmp);
qsort(t, lenT, sizeof(char), cmp);
for (int i = 0; i < lenS; i++) {
if (s[i] != t[i]) {
return false;
}
}
return true;
}