242 Valid Anagram

/*
Author: Zoro
Date:   2019/11/10
Function:   Valid Anagram
Title:  leetcode 242
anagram.c
think:  桶排序思想
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool isAnagram(char *s, char *t) {
    int statS[26] = {0};
    int statT[26] = {0};
    int lenS = strlen(s);
    int lenT = strlen(t);
    int i;
    for (i=0; i<lenS; i++) {
        int index = s[i] - 'a';
        statS[index]++;
    }
    for (i=0; i<lenT; i++) {
        int index = t[i] - 'a';
        statT[index]++;
    }
    for (i=0; i<26; i++) {
        if (statS[i] != statT[i]) {
            return false;
        }
    }
    return true;
}

int main() {
    char* s = "hello";
    char* t = "ehllo";
    printf("%d\n", isAnagram(s, t));
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xuzhaoping/p/11833987.html