LeetCode 242 Valid Anagram

LeetCode 242 Valid Anagram

 

由于都是小写字母,可以建立一个长度为26的数组来记录每个字母出现的次数。C语言实现如下:

bool isAnagram(char* s, char* t) {
    if(strlen(s)!=strlen(t))
        return 0;
    int count[26] = {0};
    char c;
    while ((c=*s++)!='\0')
        count[c-'a']++;
    while ((c=*t++)!='\0')
        count[c-'a']--;
    for (int i=0; i<26 ;++i)
        if (count[i]!=0)
            return 0;
    return 1;
}

 

posted @ 2015-11-14 10:17  Walker_Lee  阅读(140)  评论(0编辑  收藏  举报