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.
1#include <stdbool.h>
2#include <string.h>
3
4bool checkIfPangram(char *sentence) {
5 bool letters[26] = {false};
6 for (int i = 0; sentence[i] != '\0'; i++) {
7 letters[sentence[i] - 'a'] = true;
8 }
9 for (int i = 0; i < 26; i++) {
10 if (!letters[i]) return false;
11 }
12 return true;
13}
14
This solution uses a boolean array of size 26 to represent each letter of the alphabet. As we iterate through the input sentence, we mark the respective index in the array to true. Finally, we check if all elements in the array are true to verify if the sentence is a pangram.
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
Here, a boolean array is employed to track the presence of alphabet letters. The approach marks each detected letter while iterating through the sentence. A final pass ensures all alphabet indices are set to true.