forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
242.c
41 lines (31 loc) · 838 Bytes
/
242.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
bool isAnagram(char* s, char* t) {
if (s == NULL || t == NULL) return false;
int lens = strlen(s);
int lent = strlen(t);
if (lens != lent) return false;
int flag[26] = { 0 };
int i;
for (i = 0; i < lens; i++){
flag[s[i] - 'a']++;
}
for (i = 0; i < lent; i++) {
flag[t[i] - 'a']--;
}
for (i = 0; i < 26; i++){
if (flag[i] != 0) return false;
}
return true;
}
int main() {
assert(isAnagram("anagram", "nagaram") == true);
assert(isAnagram("rat", "car") == false);
assert(isAnagram("aba", "aab") == true);
assert(isAnagram("aba", "ab") == false);
printf("all tests passed!\n");
return 0;
}