判断句子是否为全字母句
全字母句 指包含英语字母表中每个字母至少一次的句子。
给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。
如果是,返回 true ;否则,返回 false 。
示例 1:
输入:sentence = “thequickbrownfoxjumpsoverthelazydog”
输出:true
解释:sentence 包含英语字母表中每个字母至少一次。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-if-the-sentence-is-pangram
思路比较简单,就是新建大小为26的数组,用来存储字母,扫描字符串,将对应的字母对应的数组值++,之后遍历一遍数组,如果有等于0的值,就返回false。
class Solution {
public boolean checkIfPangram(String sentence) {
int[] a = new int[26];
for(int i=0;i<sentence.length();i++){
a[sentence.charAt(i)-'a']++;
}
boolean flag=true;
for(int i=0;i<26;i++){
if(a[i]==0)
flag=false;
}
return flag;
}
}
今天的每日一题来的有些晚。。。