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.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public bool CheckIfPangram(string sentence) {
6 HashSet<char> seen = new HashSet<char>(sentence);
7 return seen.Count == 26;
8 }
9}
10
This C# implementation uses a HashSet to capture each unique letter in the sentence. A final check on the HashSet's size reveals if the sentence is a pangram, ensuring it includes all alphabet letters.
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
This approach relies on a 26-element boolean array, where each index corresponds to a letter of the alphabet (0 for 'a', 1 for 'b', etc.). As we parse the sentence, each letter sets its spot to true. A final loop checks that all positions in the array are true, affirming the pangram status.