1、题目
全字母句 指包含英语字母表中每个字母至少一次的句子。
给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。
如果是,返回 true ;否则,返回 false 。
示例 1:
输入:sentence = “thequickbrownfoxjumpsoverthelazydog”
输出:true
解释:sentence 包含英语字母表中每个字母至少一次。
示例 2:
输入:sentence = “leetcode”
输出:false
提示:
1 <= sentence.length <= 1000
sentence 由小写英语字母组成
2、解
直接在sentence中查找是否包含所有的小写字母。
bool chekIfPangram(string sentence)
{
for(char s = 'a' ; s < 'z' + 1; s++)
{
if(string::npos == sentence.find(s)) return false;
}
return true;
}
另解
遍历 sentence中的每个字符 c,如果 c 是字母表中的第 i (0≤i<26)字母,就将 exist[i] 置为 true。最后检查 exist 中是否存在 false,如果存在返回}false,否则返回 true。
bool chekIfPangramA(string sentence)
{
vector<int> words(26);
for(auto s : sentence){
words[s - 'a'] == 1;
}
for(auto w : words)
{
if(0 == w) return false;
}
return true;
}
另解
还可以记录sentence中每个出现过的字母,最后判断是否为26个。
bool checkIfPangramC(string sentence)
{
unordered_map<char, int> sen;
for(auto s : sentence)
{
sen[s]++;
}
return 26 == sen.size();
}