「每日LeetCode」2021年4月28日

本文最后更新于:2023年3月19日 晚上

Lt1832. 判断句子是否为全字母句

1832. 判断句子是否为全字母句

全字母句 指包含英语字母表中每个字母至少一次的句子。
给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句
如果是,返回_ true ;否则,返回 _false
示例 1:

1
2
3
输入:sentence = "thequickbrownfoxjumpsoverthelazydog"
输出:true
解释:sentence 包含英语字母表中每个字母至少一次。

示例 2:

1
2
输入:sentence = "leetcode"
输出:false

提示:

  • 1 <= sentence.length <= 1000
  • sentence 由小写英语字母组成

思路

用一个数组记录出现的情况,遍历句子得到每个字符对应的 asc 码,减去 97 以后把对应数组下标设为 0。遍历完后,如果数组内都为 0,说明每一个英文字母都出现过,返回 true。

解答

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @param {string} sentence
* @return {boolean}
*/
var checkIfPangram = function (sentence) {
const arr = new Array(26).fill(1);
for (const char of sentence) {
const code = char.charCodeAt() - 97;
arr[code] = 0;
}
return arr.every((char) => char === 0);
};