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 <= 1000sentence consists of lowercase English letters.This approach leverages a set data structure to track the unique characters. The idea is to iterate through the sentence and add each letter to a set. Finally, we check if the size of the set is 26, which indicates that all letters of the alphabet are present.
This solution uses a boolean array of size 26 to represent each letter of the alphabet. As we iterate through the input sentence, we mark the respective index in the array to true. Finally, we check if all elements in the array are true to verify if the sentence is a pangram.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the length of the sentence.
Space Complexity: O(1), since we use a fixed-size array of 26.
This method uses a boolean array of size 26 to directly map each alphabet character to an index. By iterating over each character in the sentence, we update its corresponding index in the array to true. The sentence is confirmed as a pangram if all indices in the boolean array are true.
This approach relies on a 26-element boolean array, where each index corresponds to a letter of the alphabet (0 for 'a', 1 for 'b', etc.). As we parse the sentence, each letter sets its spot to true. A final loop checks that all positions in the array are true, affirming the pangram status.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), with n denoting sentence length; we check each letter.
Space Complexity: O(1), constant size boolean array.
| Approach | Complexity |
|---|---|
| Using a Set to Track Letters | Time Complexity: O(n), where n is the length of the sentence. |
| Boolean Array for Tracking | Time Complexity: O(n), with n denoting sentence length; we check each letter. |
Google Just Hit an Absolute Low - Easy Interview Question - Check if Sentence is Pangram - 1832 • Greg Hogg • 222,685 views views
Watch 9 more video solutions →Practice Check if the Sentence Is Pangram with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor