题目

A pangram is a sentence where every letter of the English alphabet appears at least once.

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

Example 1:

1
2
3
Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.

Example 2:

1
2
Input: sentence = "leetcode"
Output: false

Constraints:

1 <= sentence.length <= 1000
sentence consists of lowercase English letters.

解题思路

这题是让我们判断字符串是不是pangram,是返回true否返回false

pangram的定义:

​ 26个小写字母字符串中都有,那么这个字符串就是pangram字符串

代码

Python

1
2
3
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
return len(set(sentence)) == 26

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
bool checkIfPangram(string sentence) {
bool alphabet[26] = {false};
int cnt = 0;
for (char c: sentence){
if (alphabet[c - 'a'] != true){
alphabet[c - 'a'] = true;
++cnt;
}
}
return cnt == 26;
}
};

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func checkIfPangram(sentence string) bool {
// create map, key->alphabet's byte, value->bool
// couting value
// return couting == 26

cnt := 0
alphabet := make(map[rune]bool)
for _, c := range sentence{
if alphabet[c] == false{
alphabet[c] = true
cnt++
}
}
return cnt == 26
}