crack the code interview 1.4

Write a method to decide if two strings are anagrams or not.

这道题就是判断两个字符串是否由相同的字符组成,写一个简单一点的。

void anagrams(char * s, chr * t)
{
    char * start = s;
    int hitS[256] = {0};
    while (*start != '\0')
    {
        int value = *start;
        hitS[value] += 1;
        start ++;
    }
    start = t;
    while (*start != '\0')
    {
        int value = * start;
        hitS[value] -= 1;
        if (hitS[value] < 0)
            return false;
    }
    for (int i = 0; i < 256; i++)
    {
        if (hitS[i] != 0)
            return false;
    }
    return true;
}


你可能感兴趣的:(crack,the,code,interview)