题目描述
全字母句 指包含英语字母表中每个字母至少一次的句子。
给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。
如果是,返回 true ;否则,返回 false 。
示例 1:
输入:sentence = “thequickbrownfoxjumpsoverthelazydog”
输出:true
解释:sentence 包含英语字母表中每个字母至少一次。
示例 2:
输入:sentence = “leetcode”
输出:false
提示:
1 <= sentence.length <= 1000
sentence 由小写英语字母组成
求解思路
- 这道题目就是一道简单的一道统计字符串词频表+去重。
实现代码
hash表:
class Solution {
public boolean checkIfPangram(String sentence) {
Set<Character> set=new HashSet<>();
for(char c:sentence.toCharArray()){
set.add(c);
}
return set.size()==26;
}
}
位运算:
class Solution {
public boolean checkIfPangram(String sentence) {
int mask = 0;
for (int i = 0; i < sentence.length(); ++i) {
mask |= 1 << (sentence.charAt(i) - 'a');
}
return mask == (1 << 26) - 1;
}
}
运行结果

本文介绍了一种算法,用于判断一个字符串是否包含英语字母表中的所有字母至少一次,即全字母句。提供了两种实现方法:使用哈希表进行字符集检查和采用位运算优化性能。
1976

被折叠的 条评论
为什么被折叠?



