Open In App

Python program to check if given string is pangram

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, write a Python program to check if that string is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.

Examples:

Input : The quick brown fox jumps over the lazy dog
Output : Yes

Input : abcdefgxyz
Output : No

We have already discussed the naive approach of pangram checking in this article. Now, let’s discuss the Pythonic approaches to do the same.

Approach #1: Pythonic Naive This method uses a loop to check if each character of the string belongs to the alphabet set or not. 

Python3




# Python3 program to
# Check if the string is pangram
import string
 
def ispangram(str):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    for char in alphabet:
        if char not in str.lower():
            return False
 
    return True
     
# Driver code
string = 'the quick brown fox jumps over the lazy dog'
if(ispangram(string) == True):
    print("Yes")
else:
    print("No")


Output:

Yes

Time Complexity: O(n), where n is the length of the given string
Auxiliary Space: O(1)

Approach #2: Using Python Set Convert the given string into set and then check if the alphabet set is greater than or equal to it or not. If the string set is greater or equal, print ‘Yes’ otherwise ‘No’. 

Python3




# Python3 program to
# Check if the string is pangram
import string
 
alphabet = set(string.ascii_lowercase)
 
def ispangram(string):
    return set(string.lower()) >= alphabet
     
# Driver code
string = "The quick brown fox jumps over the lazy dog"
if(ispangram(string) == True):
    print("Yes")
else:
    print("No")


Output:

Yes

Time complexity: O(n), where n is the length of string.
Auxiliary space: O(1), as the size of the alphabet set is constant.

Approach #3: Alternative to set method This is another method that uses Python set to find if the string is Pangram or not. We make set of lowercase alphabets and the given string. If set of given string is subtracted from the set of alphabets, we get to know whether the string is pangram or not. 

Python3




# Python3 program to
# Check if the string is pangram
import string
 
alphabet = set(string.ascii_lowercase)
 
def ispangram(str):
     return not set(alphabet) - set(str)
     
# Driver code
string = 'the quick brown fox jumps over the lazy dog'
if(ispangram(string) == True):
    print("Yes")
else:
    print("No")


Output:

Yes

Time complexity: O(1), as the code is iterating through all 26 characters of the alphabet only once.
Auxiliary space: O(1), as the code creates two sets of 26 characters each.

Approach #4 : ASCII method Check if each character of the string lies between the ASCII range of lowercase alphabets i.e. 96 to 122. 

Python3




# Python3 program to
# Check if the string is pangram
import itertools
import string
 
alphabet = set(string.ascii_lowercase)
 
def ispangram(str):
     return sum(1 for i in set(str) if 96 < ord(i) <= 122) == 26
     
# Driver code
string = 'the quick brown fox jumps over the lazy dog'
if(ispangram(string) == True):
    print("Yes")
else:
    print("No")


Output:

Yes

Time Complexity: O(n), where n is the length of the input string. The algorithm only needs to scan the input string once and check each character once, so the time complexity is linear.
Auxiliary Space: O(1), as the algorithm only uses a constant amount of extra memory to store the alphabet set and variables for the character ordinal values and the count of lowercase letters in the string.

Approach 5: Using regex:

Use regular expressions to match all the lowercase letters of the alphabet in the given string. If all the letters are present in the string, it is a pangram

Python3




import re
 
def ispangram(string):
    # Remove all non-alphabetic characters from the string and convert to lowercase
    string = re.sub('[^a-z]', '', string.lower())
    # Use set to remove duplicates and count the number of unique letters
    unique_letters = len(set(string))
    # If the number of unique letters is 26, it is a pangram
    return unique_letters == 26
 
# Driver code
string = "The quick brown fox jumps over the lazy dog"
if ispangram(string):
    print("Yes")
else:
    print("No")


Output

Yes

Time complexity: O(n), where n is the length of the input string. This is because the regular expression substitution and the set operation both take linear time proportional to the length of the string. 
Auxiliary space: O(n), because we create a new string with only alphabetic characters, which could be as long as the original string.

Approach 6: Using collections.Counter:

Step-by-step approach:

  • Import the collections module.
  • Convert the input string to lowercase and remove all non-alphabetic characters using regular expressions.
  • Use collections.Counter to count the occurrence of each alphabet in the string.
  • Check if the count of unique alphabets is equal to 26. If it is, then the string is a pangram.

Below is the implementation of the above approach:

Python3




import re
import collections
 
def ispangram(string):
    # Remove all non-alphabetic characters from the string and convert to lowercase
    string = re.sub('[^a-z]', '', string.lower())
    # Count the occurrence of each alphabet in the string
    counter = collections.Counter(string)
    # Check if the count of unique alphabets is equal to 26
    return len(counter) == 26
 
# Driver code
string = "The quick brown fox jumps over the lazy dog"
if ispangram(string):
    print("Yes")
else:
    print("No")


Output

Yes

Time complexity: O(n), where n is the length of the input string. This is because the regular expression substitution, conversion to lowercase, and counting the occurrence of each alphabet using collections.Counter all take linear time proportional to the length of the string.
Auxiliary space: O(1), as the algorithm only uses a constant amount of extra memory to store the alphabet set and the collections.Counter object.



Last Updated : 23 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads