原始问题:
判断由小写英文字母组成的字符串中每个字母是否至少出现一次
以下是C语言的代码实现:
#include <stdio.h>
#include <stdbool.h>
bool checkSentence(char sentence[]) {
int count[26] = {0}; // 用于记录每个字母出现的次数
int i = 0;
while (sentence[i] != '\0') {
if (sentence[i] >= 'a' && sentence[i] <= 'z') {
count[sentence[i] - 'a']++; // 对应字母出现次数加1
}
i++;
}
for (i = 0; i < 26; i++) {
if (count[i] == 0) {
return false; // 存在字母未出现
}
}
return true; // 所有字母均出现
}
int main() {
char sentence1[] = "thequickbrownfoxjumpsoverthelazydog";
char sentence2[] = "leetcode";
printf("%s
", checkSentence(sentence1) ? "true" : "false");
printf("%s
", checkSentence(sentence2) ? "true" : "false");
return 0;
}代码中使用了一个长度为26的数组count来记录每个字母出现的次数,遍历字符串时,如果当前字符是小写字母,则对应字母的计数器加1。最后再遍历一遍count数组,如果存在计数器为0的字母,则说明该字母未出现,返回false;否则返回true。
Prev:爬虫爬取哔哩哔哩代码