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.
Clarifying Questions
- Input Type: What is the type of the input?
- The input is a string containing only lowercase English letters.
- Output Type: What is the type of the output?
- The output is a boolean value, either
True
or False
.
- Constraints:
- The length of the sentence could be up to (10^4).
- The string contains only lowercase English letters.
Strategy
- Use a Set for Uniqueness: Since we need each letter to appear at least once, we can utilize a set to keep track of distinct letters encountered in the string.
- Check Set Length: After processing the string, if the size of the set is 26 (the number of letters in the English alphabet), then the string is a pangram.
- Efficiency:
- Insertion into a set and checking its length is efficient.
- We traverse the string once, making the overall time complexity linear.
Code
def checkIfPangram(sentence: str) -> bool:
# A set to track all unique letters in the sentence
seen_letters = set()
# Iterate through each character in the sentence
for char in sentence:
seen_letters.add(char)
# Check if we have all 26 letters
return len(seen_letters) == 26
# Example usage
sentence = "thequickbrownfoxjumpsoverthelazydog"
print(checkIfPangram(sentence)) # Output: True
Time Complexity
- Time Complexity: (O(n)), where (n) is the length of the input string. Each character is processed once and inserting into the set is on average (O(1)).
- Space Complexity: (O(1)), because the set will hold at most 26 characters regardless of the input size.
Thus, this solution is efficient and straightforward for checking if a sentence is a pangram.
-
Got blindsided by a question you didn’t expect?
-
Spend too much time studying?
-
Or simply don’t have the time to go over all 3000 questions?