跳转至

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

题目描述

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

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

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

 

示例 1:

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

示例 2:

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

 

提示:

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

解法

方法一:数组或哈希表

遍历字符串 sentence,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有 $26$ 个字母即可。

时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 sentence 的长度,而 $C$ 为字符集大小。本题中 $C = 26$。

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
13
14
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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
}
1
2
3
4
5
6
7
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);
}
1
2
3
4
5
6
7
8
9
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)
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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;
}

方法二:位运算

我们也可以用一个整数 $mask$ 记录出现过的字母,其中 $mask$ 的第 $i$ 位表示第 $i$ 个字母是否出现过。

最后判断 $mask$ 的二进制表示中是否有 $26$ 个 $1$,也即判断 $mask$ 是否等于 $2^{26} - 1$。若是,返回 true,否则返回 false

时间复杂度 $O(n)$,其中 $n$ 为字符串 sentence 的长度。空间复杂度 $O(1)$。

1
2
3
4
5
6
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
1
2
3
4
5
6
7
8
9
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;
    }
}
1
2
3
4
5
6
7
8
class Solution {
public:
    bool checkIfPangram(string sentence) {
        int mask = 0;
        for (char& c : sentence) mask |= 1 << (c - 'a');
        return mask == (1 << 26) - 1;
    }
};
1
2
3
4
5
6
7
func checkIfPangram(sentence string) bool {
    mask := 0
    for _, c := range sentence {
        mask |= 1 << int(c-'a')
    }
    return mask == 1<<26-1
}
1
2
3
4
5
6
7
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;
}
1
2
3
4
5
6
7
8
9
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
    }
}
1
2
3
4
5
6
7
bool checkIfPangram(char* sentence) {
    int mark = 0;
    for (int i = 0; sentence[i]; i++) {
        mark |= 1 << (sentence[i] - 'a');
    }
    return mark == (1 << 26) - 1;
}

评论