【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;
}
相关推荐
aaaameliaaa4 小时前
字符函数和字符串函数
c语言·笔记·算法
夜月yeyue4 小时前
AUTOSAR CP 从上电到 Runnable
c语言·网络·tcp/ip·车载系统
城管不管5 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯6 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡6 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯6 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang6 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9856 小时前
高进度算法模板速记(待完善)
java·前端·算法
小羊先生car7 小时前
RTOS-F429-HAL-绝对延时和相对延时(2026/7/31)
c语言·rtos
圣保罗的大教堂9 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode