comments | difficulty | edit_url | rating | source | tags | ||
---|---|---|---|---|---|---|---|
true |
简单 |
1166 |
第 237 场周赛 Q1 |
|
全字母句 指包含英语字母表中每个字母至少一次的句子。
给你一个仅由小写英文字母组成的字符串 sentence
,请你判断 sentence
是否为 全字母句 。
如果是,返回 true
;否则,返回 false
。
示例 1:
输入:sentence = "thequickbrownfoxjumpsoverthelazydog"
输出:true
解释:sentence
包含英语字母表中每个字母至少一次。
示例 2:
输入:sentence = "leetcode" 输出:false
提示:
1 <= sentence.length <= 1000
sentence
由小写英语字母组成
遍历字符串 sentence
,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有
时间复杂度 sentence
的长度,而
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
return len(set(sentence)) == 26
class Solution {
public boolean checkIfPangram(String sentence) {
boolean[] vis = new boolean[26];
for (int i = 0; i < sentence.length(); ++i) {
vis[sentence.charAt(i) - 'a'] = true;
}
for (boolean v : vis) {
if (!v) {
return false;
}
}
return true;
}
}
class Solution {
public:
bool checkIfPangram(string sentence) {
int vis[26] = {0};
for (char& c : sentence) vis[c - 'a'] = 1;
for (int& v : vis)
if (!v) return false;
return true;
}
};
func checkIfPangram(sentence string) bool {
vis := [26]bool{}
for _, c := range sentence {
vis[c-'a'] = true
}
for _, v := range vis {
if !v {
return false
}
}
return true
}
function checkIfPangram(sentence: string): boolean {
const vis = new Array(26).fill(false);
for (const c of sentence) {
vis[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
return vis.every(v => v);
}
impl Solution {
pub fn check_if_pangram(sentence: String) -> bool {
let mut vis = [false; 26];
for c in sentence.as_bytes() {
vis[(*c - b'a') as usize] = true;
}
vis.iter().all(|v| *v)
}
}
bool checkIfPangram(char* sentence) {
int vis[26] = {0};
for (int i = 0; sentence[i]; i++) {
vis[sentence[i] - 'a'] = 1;
}
for (int i = 0; i < 26; i++) {
if (!vis[i]) {
return 0;
}
}
return 1;
}
我们也可以用一个整数
最后判断 true
,否则返回 false
。
时间复杂度 sentence
的长度。空间复杂度
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
mask = 0
for c in sentence:
mask |= 1 << (ord(c) - ord('a'))
return mask == (1 << 26) - 1
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;
}
}
class Solution {
public:
bool checkIfPangram(string sentence) {
int mask = 0;
for (char& c : sentence) mask |= 1 << (c - 'a');
return mask == (1 << 26) - 1;
}
};
func checkIfPangram(sentence string) bool {
mask := 0
for _, c := range sentence {
mask |= 1 << int(c-'a')
}
return mask == 1<<26-1
}
function checkIfPangram(sentence: string): boolean {
let mark = 0;
for (const c of sentence) {
mark |= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
return mark === (1 << 26) - 1;
}
impl Solution {
pub fn check_if_pangram(sentence: String) -> bool {
let mut mark = 0;
for c in sentence.as_bytes() {
mark |= 1 << (*c - b'a');
}
mark == (1 << 26) - 1
}
}
bool checkIfPangram(char* sentence) {
int mark = 0;
for (int i = 0; sentence[i]; i++) {
mark |= 1 << (sentence[i] - 'a');
}
return mark == (1 << 26) - 1;
}