跳转至

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

问题描述#

全字母句 指包含英语字母表中每个字母至少一次的句子。

给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句

如果是,返回 true ;否则,返回 false

 

示例 1:


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

示例 2:


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

 

提示:

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

解题思路#

1
2
3
class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        return len(set(sentence)) == 26
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    bool checkIfPangram(string s) {
        int a = 0;
        for (char c : s) {
            if ('a' <= c && c <= 'z') {
                a |= 1 << (c - 'a');
            }
        }
        return a == (1 << 26) - 1;
    }
};

时间复杂度\(\mathcal{O}(n)\)
空间复杂度\(\mathcal{O}(1)\)

返回顶部

在手机上阅读