The Wayback Machine - https://web.archive.org/web/20240930191228/https://www.geeksforgeeks.org/python-set-check-string-panagram/
Open In App

Python set to check if string is pangram

Last Updated : 25 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a string, check if the given string is a pangram or not.

Examples: 

Input : The quick brown fox jumps over the lazy dog
Output : The string is a pangram

Input : geeks for geeks
Output : The string is not pangram

A normal way would have been to use frequency table and check if all elements were present or not. But using import ascii_lowercase as asc_lower we import all the lower characters in set and all characters of string in another set. In the function, two sets are formed- one for all lower case letters and one for the letters in the string. The two sets are subtracted and if it is an empty set, the string is a pangram.

Below is Python implementation of the above approach: 

Python




# import from string all ascii_lowercase and asc_lower
from string import ascii_lowercase as asc_lower
 
# function to check if all elements are present or not
def check(s):
    return set(asc_lower) - set(s.lower()) == set([])
     
# driver code
string ="The quick brown fox jumps over the lazy dog"
if(check(string)== True):
    print("The string is a pangram")
else:
    print("The string isn't a pangram")


Output

The string is a pangram

Time Complexity: O(n)
Auxiliary Space: O(1) 

Method #2: Using lower(),replace(),list(),set(),sort() and join() methods

Python3




# function to check if all elements are present or not
 
string ="The quick brown fox jumps over the lazy dog"
string=string.replace(" ","")
string=string.lower()
x=list(set(string))
x.sort()
x="".join(x)
alphabets="abcdefghijklmnopqrstuvwxyz"
if(x==alphabets):
    print("The string is a pangram")
else:
    print("The string isn't a pangram")


Output

The string is a pangram

Time Complexity : O(N logN)
Auxiliary Space : O(N)

Method #3 : Using lower(),replace(),len() methods and for loop

Approach

  1. Convert the given string to lowercase and remove all spaces
  2. Check whether all the alphabets are present in given string by incrementing count variable and for loop
  3. If count is equal to 26(number of alphabets) display The string is a pangram
  4. If not display The string isn’t a pangram

Python3




# function to check if all elements are present or not
 
string ="The quick brown fox jumps over the lazy dog"
string=string.replace(" ","")
string=string.lower()
alphabets="abcdefghijklmnopqrstuvwxyz"
c=0
for i in alphabets:
    if i in string:
        c+=1
if(c==len(alphabets)):
    print("The string is a pangram")
else:
    print("The string isn't a pangram")


Output

The string is a pangram

Time Complexity : O(N) N – total number of alphabets

Auxiliary Space : O(1) 



Similar Reads

C++ program to check whether a String is a Pangram or not
Given string str, the task is to check whether a string is pangram or not using in C++. A string is a Pangram if the string contains all the English alphabet letters. Examples: Input: str = "We promptly judged antique ivory buckles for the next prize" Output: Yes Explanations: In the above string, str has all the English alphabet letters. Input: st
6 min read
Check if a String can be converted to Pangram in K changes
Given a String str containing only lowercase English alphabets and an integer K. The task is to check that whether the string can be converted to a Pangram by performing at most K changes. In one change we can remove any existing character and add a new character. Pangram: A pangram is a sentence containing every letter in the English Alphabet. Not
6 min read
Check if given String is Pangram or not
Given a string Str. The task is to check if it 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: is a Pangram Explanation: Contains all the characters from ‘a’ to ‘z’] Input: "The quick brown fox jumps over the dog"Output: is not a Pangr
13 min read
Using Set() in Python Pangram Checking
Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Lowercase and Uppercase are considered the same. Examples: Input : str = 'The quick brown fox jumps over the lazy dog' Output : Yes // Contains all the characters from ‘a’ to ‘z’ Input : str='The quick brown fox jumps over the dog'
3 min read
Check if a number is a Pangram or not
Given an integer N, the task is to check whether the given number is a pangram or not. Note: A Pangram Number contains every digit [0- 9] at least once. Examples: Input : N = 10239876540022Output : YesExplanation: N contains all the digits from 0 to 9. Therefore, it is a pangram. Input : N = 234567890Output : NoExplanation: N doesn't contain the di
7 min read
Cost to make a string Pangram
Given an array arr[] containing the cost of adding each alphabet from (a - z) and a string str which may or may not be a Pangram. The task is to find the total cost to make the string Pangram. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}, str = "abcdefghijklmopqrstuvwz " Ou
12 min read
Missing characters to make a string Pangram
Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string, i.e., the characters that can make the string a Pangram. We need to print output in alphabetic order. Examples: Input : welcome to geeksforgeeksOutput : abdhijnpquvxyzInput : The quick brown fox jumpsOutput :
8 min read
Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
Given a tree, and the weights (in the form of strings) of all the nodes, the task is to count the nodes whose weighted string when concatenated with the strings of the sub-tree nodes becomes a pangram. Pangram: A pangram is a sentence containing every letter of the English Alphabet. Examples: Input: Output: 1 Only the weighted string of sub-tree of
7 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
Check if both halves of the string have same set of characters in Python
Given a string of lowercase characters only, the task is to check if it is possible to split a string from middle which will gives two halves having the same characters and same frequency of each character. If the length of the given string is ODD then ignore the middle element and check for the rest. Examples: Input : abbaab Output : NO The two ha
3 min read
Python Set | Check whether a given string is Heterogram or not
Given a string S of lowercase characters. The task is to check whether a given string is a Heterogram or not using Python. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. Input1: S = "the big dwarf only jumps"Output1: YesExplanation: Each alphabet in the string S is occurred only once.Input2: S
3 min read
Check whether second string can be formed from characters of first string
Given two strings str1 and str2, check if str2 can be formed from str1 Example : Input : str1 = geekforgeeks, str2 = geeksOutput : YesHere, string2 can be formed from string1. Input : str1 = geekforgeeks, str2 = andOutput : NoHere string2 cannot be formed from string1. Input : str1 = geekforgeeks, str2 = geeeekOutput : YesHere string2 can be formed
5 min read
Check if a string can be formed from another string using given constraints
Given two strings S1 and S2(all characters are in lower-case). The task is to check if S2 can be formed from S1 using given constraints: 1. Characters of S2 is there in S1 if there are two 'a' in S2, then S1 should have two 'a' also. 2. If any character of S2 is not present in S1, check if the previous two ASCII characters are there in S1. e.g., if
7 min read
Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
Given two strings, the task is to check whether the frequencies of a character(for each character) in one string are multiple or a factor in another string. If it is, then output "YES", otherwise output "NO". Examples: Input: s1 = "aabccd", s2 = "bbbaaaacc" Output: YES Frequency of 'a' in s1 and s2 are 2 and 4 respectively, and 2 is a factor of 4 F
6 min read
Check if a string can be converted to another string by replacing vowels and consonants
Given two strings S1 and S2, you are allowed only to change a character at any position to any vowel if it is a vowel or to a consonant, if it is a consonant. The task is to check if a string S1 can be changed to S2 or S2 can be changed to S1. Examples: Input: S1 = "abcgle", S2 = "ezggli" Output: Yes Change 'a' to 'e', 'b' to 'z', 'c' to 'g' and 'e
6 min read
Queries to check if string B exists as substring in string A
Given two strings A, B and some queries consisting of an integer i, the task is to check whether the sub-string of A starting from index i and ending at index i + length(B) - 1 equals B or not. If equal then print Yes else print No. Note that i + length(B) will always be smaller than length(A). Examples: Input: A = "abababa", B = "aba", q[] = {0, 1
15+ min read
Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
9 min read
Check if String formed by first and last X characters of a String is a Palindrome
Given a string str and an integer X. The task is to find whether the first X characters of both string str and reversed string str are same or not. If it is equal then print true, otherwise print false. Examples: Input: str = abcdefba, X = 2Output: trueExplanation: First 2 characters of both string str and reversed string str are same. Input: str =
5 min read
Check if the given string is shuffled substring of another string
Given strings str1 and str2. The task is to find if str1 is a substring in the shuffled form of str2 or not. Print "YES" if str1 is a substring in shuffled form of str2 else print "NO". Example Input: str1 = "onetwofour", str2 = "hellofourtwooneworld" Output: YES Explanation: str1 is substring in shuffled form of str2 as str2 = "hello" + "fourtwoon
15 min read
Check if a given string is a Reverse Bitonic String or not
Given a string str, the task is to check if that string is a Reverse Bitonic string or not. If the string str is reverse Bitonic string, then print "YES". Otherwise, print "NO". A Reverse Bitonic String is a string in which the characters are arranged in decreasing order followed by increasing order of their ASCII values. Examples: Input: str = "zy
6 min read
Check if a string can be obtained by appending subsequences of another string
Given two strings str1 and str2 of lengths N and M respectively, the task is to check if str2 can be formed by appending subsequences of str1 multiple times. If possible, print the minimum number of append operations required. Otherwise, print -1. Examples: Input: str1 = "abb", str2 = "ababbbbb"Output: 4Explanation:String str2 can be formed by appe
8 min read
Check if string S2 can be obtained by appending subsequences of string S1
Given two strings S1 and S2, the task is to check if it's possible to generate string S2 by repeatedly appending subsequences of S1 to the end of an initially empty string. If possible, print "YES" and the minimum number of operations required. Otherwise, print "NO". Examples: Input: S1 = "acebcd", S2 = "acbcd" Output: YES 2 Explanation: Append sub
10 min read
Check if a string can be made equal to another string by swapping or replacement of characters
Given two strings S1 and S2, the task is to check if string S1 can be made equal to string S2 by swapping any pair of characters replacing any character in the string S1. If it is possible to make the string S1 equal to S2, then print "Yes". Otherwise, print "No". Examples: Input: S1 = “abc”, S2 = “bca”Output: YesExplanation:Operation 1: "abc" -
8 min read
Check if a string can be converted to another given string by removal of a substring
Given two strings S and T of length N and M respectively, the task is to check if the string S can be converted to the string T by removing at most one substring of the string S. If found to be true, then print “YES”. Otherwise, print “NO”. Example: Input: S = “abcdef”, T = “abc” Output: YES Explanation: Removing the substring { S[3], …, S[5] } mod
7 min read
Check if a string contains an anagram of another string as its substring
Given two strings S1 and S2, the task is to check if S2 contains an anagram of S1 as its substring. Examples: Input: S1 = "ab", S2 = "bbpobac"Output: YesExplanation: String S2 contains anagram "ba" of S1 ("ba"). Input: S1 = "ab", S2 = "cbddaoo"Output: No Approach: Follow the steps below to solve the problem: Initialize two Hashmaps s1hash and s2has
7 min read
Check if string A can be converted to string B by changing A[i] to A[i+1] or A[i]..A[i+K-1] to A[i]+1 each
Given two strings A and B each of length N and an integer K, the task is to find if string A can be converted to string B, using the following operations any number of times: Type1: Choose index i, and swap Ai and Ai+1Type2: Choose index i, and if Ai, Ai+1, ..., Ai+K-1 are all equal to some character ch ( ch ≠ z ), replace each character with its n
8 min read
Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts
Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a" - Shift 3 times - "d" Character "b" - Shift 2 ti
3 min read
Javascript Program to Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
4 min read
Check if string S1 can be formed using repeated insertions of another string S2
Given two strings S1 and S2 consisting of unique characters, the task is to check S1 can be formed by repeated insertions of string S2. Input: S1 = "aabb", S2 = "ab"Output: YesExplanation: the mentioned string can be obtained after series of moves: Insert string "ab" in an empty string. Current string will be "ab"insert "ab" after "a". The final st
7 min read
Check if any permutation of a given string is lexicographically larger than the other given string
Given two strings str1 and str2 of same length N, the task is to check if there exists any permutation possible in any of the given strings, such that every character of one string is greater or equal to every character of the other string, at corresponding indices. Return true if permutation exists otherwise false. Example: Input: str1 = "adb", st
10 min read
Article Tags :
Practice Tags :