blob: e7f7c45f679e902fb4b253f4519827bfadbafbf4 (
plain)
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
|
// 242. Valid Anagram
#include "leetcode.h"
/*
* given two strings 's' and 't', return 'true' if 't' is an anagram of 's', and
* false otherwise. an anagram is a word or phrase formed by rearranging the
* letters of a different word or phrase, typically using all the original
* letters exactly once.
*/
bool isAnagram(char *s, char *t) {
if (strlen(s) != strlen(t))
return 0;
int mask[256] = {};
char *c = s;
while (*c)
mask[*c++]++;
c = t;
while (*c) {
if (mask[*c])
mask[*c++]--;
else
return 0;
}
return 1;
}
int main() {
char *s1 = "anagram", *t1 = "nagaram";
char *s2 = "rat", *t2 = "car";
printf("%d\n", isAnagram(s1, t1)); // expect: true
printf("%d\n", isAnagram(s2, t2)); // expect: false
}
|