Sponsored
Sponsored
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.
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.
1import java.util.HashSet;
2
3public class Solution {
4 public boolean checkIfPangram(String sentence) {
5 HashSet<Character> seen = new HashSet<>();
6 for (char ch : sentence.toCharArray()) {
7 seen.add(ch);
8 }
9 return seen.size() == 26;
10 }
11}
12
This Java implementation uses a HashSet to maintain track of identified letters. As we traverse the sentence, each letter is added to the HashSet. The size of the HashSet will confirm if the sentence contains all 26 letters of the alphabet.
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.
Time Complexity: O(n), with n denoting sentence length; we check each letter.
Space Complexity: O(1), constant size boolean array.
1 public bool CheckIfPangram(string sentence) {
bool[] alphabet = new bool[26];
foreach (char ch in sentence) {
alphabet[ch - 'a'] = true;
}
foreach (bool b in alphabet) {
if (!b) return false;
}
return true;
}
}
C# caters to a boolean array indicating the usage of each alphabet letter within the input text. Each letter marks its index to true while a conclusive loop guarantees a complete alphabet presence.