LeetCode · April 28, 2021

Check if the Sentence Is Pangram | LeetCode 1832 | Python | Solution

Click here to see the problem details.

It’s an easy LeetCode problem with straightforward problem details.

In a simple word, we will be given a sentence to check it is Pangram or not. So, what is Pangram? According to the problem description, “A pangram is a sentence where every letter of the English alphabet appears at least once.”

One more thing, The given sentence contains only lowercase letters. So, we don’t have to check uppercase.

There are many ways to solve this problem. Let’s see one from them.

Solution One

This way, we will store the all lowercase English alphabet in a variable as a string. Then we will run a loop through this variable. Inside the loop, we will check the current letter is in the given sentence or not. If the current letter is not in the given sentence, we will return False. Otherwise, we will return True after the loop. Pretty simple.

Let’s see in code for better understanding.

class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
        
        for let in alphabet:
            if let not in sentence:
                return False
        
        return True

Solution Two

Let’s see another way to solve this problem, which is faster than the previous one.

This solution is pretty concise.

We all know there are 26 English letters.

If we use the set function and pass the given sentence as a parameter, it will return only distinct elements. Then we will compare the length of it with the 26.

We will easily get our result by comparing these two numbers.

Let’s see the code.

class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        
        return len(set(sentence)) == 26

That’s all about the problem solution. I hope it will help you.