1832. Check if the Sentence Is Pangram
Description
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:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog" Output: true Explanation: sentence contains at least one of every letter of the English alphabet.
Example 2:
Input: sentence = "leetcode" Output: false
Constraints:
1 <= sentence.length <= 1000
sentence
consists of lowercase English letters.
Solutions
Solution 1: Array or Hash Table
Traverse the string sentence
, use an array or hash table to record the letters that have appeared, and finally check whether there are $26$ letters in the array or hash table.
The time complexity is $O(n)$, and the space complexity is $O(C)$. Where $n$ is the length of the string sentence
, and $C$ is the size of the character set. In this problem, $C = 26$.
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Solution 2: Bit Manipulation
We can also use an integer $mask$ to record the letters that have appeared, where the $i$-th bit of $mask$ indicates whether the $i$-th letter has appeared.
Finally, check whether there are $26$ $1$s in the binary representation of $mask$, that is, check whether $mask$ is equal to $2^{26} - 1$. If so, return true
, otherwise return false
.
The time complexity is $O(n)$, where $n$ is the length of the string sentence
. The space complexity is $O(1)$.
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 |