自学内容网 自学内容网

【C语言刷力扣】1832.判断句子是否为全字母句

题目:

法一 

bool checkIfPangram(char* sentence) {
    int str[256];
    memset(str, 0, sizeof(int));
    for (int i = 0; i < strlen(sentence); ++i) {
        ++str[ sentence[i] ];
    }

    for (int j = 'a'; j <= 'z'; ++j) {
        if (!str[j]) return false;
    }
    return true;
}

法二 动态分配

typedef struct {
    char word;
    int count;
    UT_hash_handle hh;
} hashEntry;

bool checkIfPangram(char* sentence) {
    hashEntry * cnt = NULL;
    for (int i = 0; i < strlen(sentence); i++) {
        hashEntry* pEntry = NULL;
        HASH_FIND(hh, cnt, &sentence[i], sizeof(char), pEntry);
        if (pEntry == NULL) {
            hashEntry * pEntry = (hashEntry*)malloc(sizeof(hashEntry));
            pEntry -> word = sentence[i];
            pEntry -> count = 1;
            HASH_ADD(hh, cnt, word, sizeof(char), pEntry);
        }
        else pEntry -> count++;
    }

    hashEntry *curr = NULL, *next = NULL;
    int num = 26;
    HASH_ITER(hh, cnt, curr, next)
    {
        if (curr -> count > 0) {
            num--;
        }
        if (num == 0) return true;
        free(curr);
    }
    return false;
}


原文地址:https://blog.csdn.net/2301_76779875/article/details/142986255

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!