Leetcode 1832. Check if the Sentence Is Pangram Solution

Problem Description

A pangram is a sentence where every letter of the English alphabet appears at least once. The goal of this problem is to determine whether a given string sentence, which consists of only lowercase English letters, is a pangram or not. If it is a pangram, the function should return true, otherwise, it should return false.

Example

Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
Output: true
Explanation: The sentence contains at least one of every letter of the English alphabet, thus it is a pangram.

Input: sentence = "leetcode"
Output: false
Explanation: The sentence does not contain every letter of the English alphabet, thus it is not a pangram.

Constraints

  • 1 <= sentence.length <= 1000
  • sentence consists of lowercase English letters.

Solution Approach

The approach for this problem is very simple. We need to check if the input sentence contains all 26 lowercase English letters. To do this, we create a set and insert all the unique letters in the sentence. The sentence is a pangram if the size of the set is equal to 26, which means that all English letters are present in the sentence.

Python Solution

1class Solution:
2    def checkIfPangram(self, sentence: str) -> bool:
3        # Create a set and insert all unique letters in the sentence
4        unique_letters = set(sentence)
5        # If the size of the set is equal to 26, the sentence is a pangram
6        return len(unique_letters) == 26

Java Solution

1class Solution {
2    public boolean checkIfPangram(String sentence) {
3        // Create a HashSet and insert all unique letters in the sentence
4        HashSet<Character> uniqueLetters = new HashSet<>();
5        for(char c : sentence.toCharArray()){
6            uniqueLetters.add(c);
7        }
8        // If the size of the set is equal to 26, the sentence is a pangram
9        return uniqueLetters.size() == 26;
10    }
11}

JavaScript Solution

1class Solution {
2    checkIfPangram(sentence) {
3        // Create a Set and insert all unique letters in the sentence
4        const uniqueLetters = new Set(sentence);
5        // If the size of the set is equal to 26, the sentence is a pangram
6        return uniqueLetters.size === 26;
7    }
8}

C++ Solution

1class Solution {
2public:
3    bool checkIfPangram(string sentence) {
4        // Create a set and insert all unique letters in the sentence
5        unordered_set<char> unique_letters(begin(sentence), end(sentence));
6        // If the size of the set is equal to 26, the sentence is a pangram
7        return unique_letters.size() == 26;
8    }
9};

C# Solution

1public class Solution {
2    public bool CheckIfPangram(string sentence) {
3        // Create a HashSet and insert all unique letters in the sentence
4        HashSet<char> uniqueLetters = new HashSet<char>(sentence);
5        // If the size of the set is equal to 26, the sentence is a pangram
6        return uniqueLetters.Count == 26;
7    }
8}

Conclusion

In conclusion, we can solve the pangram problem by creating a set or hash set to store unique letters from the input sentence. Checking if all English letters are present in the sentence can be done by verifying if the size of the set equals 26. The solutions provided above in Python, JavaScript, Java, C++, and C# use this approach to solve the problem efficiently and in a straightforward manner.