【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;
}
相关推荐
肉夹馍不加青椒10 分钟前
第三十三天(信号量)
java·c语言·算法
古译汉书43 分钟前
嵌入式-SPI番外之按钮驱动程序的编写-Day15
c语言·stm32·单片机·嵌入式硬件·mcu·算法
knd_max1 小时前
C语言:字符函数与字符串函数(1)
c语言
快去睡觉~1 小时前
力扣48:旋转矩阵
算法·leetcode·矩阵
卡洛斯(编程版3 小时前
(1) 哈希表全思路-20天刷完Leetcode Hot 100计划
python·算法·leetcode
444A4E3 小时前
深入理解Linux进程管理:从创建到替换的完整指南
linux·c语言·操作系统
NAGNIP3 小时前
DeepSeekMoE 架构解析
算法
不喜欢学数学er3 小时前
算法第五十二天:图论part03(第十一章)
算法·深度优先·图论
养成系小王3 小时前
四大常用排序算法
数据结构·算法·排序算法
NAGNIP4 小时前
一文搞懂DeepSeek LLM
算法