0% completed
Problem Statement
Given a string sentence containing English letters (lower- or upper-case), return true if sentence is a Pangram, or false otherwise.
A Pangram is a sentence where every letter of the English alphabet appears at least once.
Note: The given sentence might contain other characters like digits or spaces, your solution should handle these too.
Example 1:
Input: sentence = "TheQuickBrownFoxJumpsOverTheLazyDog"
Output: true
Explanation: The sentence contains at least one occurrence of every letter of the English alphabet either in lower or upper case.
Example 2:
Input: sentence = "This is not a pangram"
Output: false
Explanation: The sentence doesn't contain at least one occurrence of every letter of the English alphabet.
Constraints:
1 <= sentence.length <= 1000sentenceconsists of lower or upper-case English letters.
Solution
We can use a HashSet to check if the given sentence is a pangram or not. The HashSet will be used to store all the unique characters in the sentence. The algorithm works as follows:
- Define
seenhashSet to store all unique characters of the string. - Iterate over each character of the sentence using a loop.
- Convert the character at index
ito the lowercase letter, and store it in thecurrCharvariable. - If
currCharis alphabetical letter, add it in theseenhashSet. - Add each character to the
HashSet. - After looping through all characters, compare the size of the
HashSetwith 26 (total number of alphabets). If the size of the HashSet is equal to 26, it means the sentence contains all the alphabets and is a pangram, so the function will return true. Otherwise, it will return false.
Code
Here is the code for this algorithm:
Time Complexity
-
Iterating Over Characters: The main operation in the code is iterating over each character in the input string. If the length of the input string is
n, this iteration occursntimes. -
Set Operations: For each character, the code performs a constant-time operation—adding the character to a HashSet if it's a letter. The time complexity for adding an element to a HashSet is typically O(1).
-
Overall Time Complexity: Considering the iteration over
ncharacters and constant-time set operations, the total time complexity is O(n), wherenis the length of the sentence.
Space Complexity
-
HashSet Storage: The HashSet
seenis used to store the distinct characters encountered in the sentence. In the worst-case scenario, it will store all26letters of the alphabet. -
Constant Size Set: Regardless of the input sentence length, the HashSet can only grow up to a size of
26. This is because it only stores distinct English alphabet letters. -
Overall Space Complexity: Given the HashSet's maximum size is constant (at most
26characters), the space complexity is O(1), meaning it is constant.
Conclusion
- Time Complexity: O(n), where
nis the length of the input string. - Space Complexity: O(1) (constant space, independent of input string length).
On this page
Problem Statement
Solution
Code
Time Complexity
Space Complexity
Conclusion