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, determine if sentence
is a pangram.
Examples:
sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
(Contains every letter of the English alphabet).
sentence = "leetcode"
Output: false
(Does not contain every letter of the English alphabet).
Constraints:
1 <= sentence.length <= 1000
sentence
consists of lowercase English letters.Could you provide an algorithm to solve this problem efficiently? What is the time and space complexity of your solution? Are there any edge cases to consider?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to determine if a sentence contains every letter of the alphabet. The brute force approach will check for the presence of each letter individually, one by one, in the provided sentence.
Here's how the algorithm would work step-by-step:
def is_pangram_brute_force(input_string):
alphabet_string = "abcdefghijklmnopqrstuvwxyz"
for character in alphabet_string:
# Check each character in alphabet
found = False
for input_character in input_string:
# Iterate over the input string to check presence
if character == input_character.lower():
found = True
break
# If alphabet character isn't found, it isn't a pangram
if not found:
return False
return True
To check if a sentence is a pangram, we want to see if it contains all the letters of the alphabet. The most efficient way is to keep track of which letters we've seen and quickly determine if all 26 are present.
Here's how the algorithm would work step-by-step:
def is_pangram(input_string):
alphabet_present = [False] * 26
for char in input_string:
if 'a' <= char <= 'z':
# Adjust index to map 'a' to 0, 'b' to 1, etc.
index = ord(char) - ord('a')
alphabet_present[index] = True
elif 'A' <= char <= 'Z':
index = ord(char) - ord('A')
alphabet_present[index] = True
# Verify that each letter of the alphabet has appeared
for present in alphabet_present:
if not present:
return False
return True
Case | How to Handle |
---|---|
Empty or null sentence input | Return false immediately as an empty sentence cannot be a pangram. |
Sentence containing characters other than English letters (e.g., numbers, symbols, spaces) | Ignore non-letter characters during alphabet presence check, focusing only on 'a'-'z' or 'A'-'Z'. |
Sentence containing only uppercase letters | Convert all letters to lowercase before checking for alphabet completeness to ensure case-insensitivity. |
Sentence containing only lowercase letters | Process directly after potential conversion to lowercase as part of general algorithm. |
Sentence is very long (large input size) | Use a boolean array or set to track the presence of each alphabet letter, ensuring efficient O(n) time complexity despite large input. |
Sentence containing duplicate letters, with some letters missing | The set or boolean array ensures duplicates do not falsely indicate completeness, correctly identifying the missing letters. |
Sentence consisting of only spaces or other non-alphabetic characters | After stripping or ignoring non-alphabetic characters, the sentence should be treated like an empty string. |
Sentence containing Unicode characters outside the basic English alphabet | Filter out Unicode characters that are not within the English alphabet range before the alphabet check. |