#1832
Check if the Sentence Is Pangram
EasyHash TableStringHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
The optimal approach uses a set to store unique characters from the sentence. By adding each character to the set, we can easily check if we have all 26 letters by the end. This is efficient because checking membership in a set is fast.
⚙️
Algorithm
3 steps- 1Step 1: Initialize an empty set to store unique characters.
- 2Step 2: Iterate through each character in the sentence and add it to the set.
- 3Step 3: After processing the sentence, check if the size of the set is 26. If yes, return true; otherwise, return false.
solution.py2 lines
1def checkIfPangram(sentence):
2 return len(set(sentence)) == 26ℹ
Complexity note: The time complexity is O(n) because we traverse the sentence once, and the space complexity is O(n) due to the storage of unique characters in the set.
- 1Every letter must be present at least once.
- 2Using a set helps track unique characters efficiently.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.