diff options
Diffstat (limited to 'valid_anagram.c')
-rw-r--r-- | valid_anagram.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/valid_anagram.c b/valid_anagram.c new file mode 100644 index 0000000..e7f7c45 --- /dev/null +++ b/valid_anagram.c @@ -0,0 +1,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 +} |