Python Set | Check whether a given string is Heterogram or not Last Updated : 01 Sep, 2023 Comments Improve Suggest changes Like Article Like Report 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 = "geeksforgeeks" Output2: NoExplanation: Since alphabet 'g', 'e', 'k', 's' occurred more than once.We have an existing solution for this problem. Please refer to Check whether a given string is a Heterogram or not link. Some other solutions for this problem are given below. Python program to check whether a given string is Heterogram or notBelow are the approaches using which we can check whether a given string is Heterogram or not: Using setUsing lower() + isalpha()Check Whether a Given String is Heterogram using SetThe approach is to check sentence is a heterogram or not. We are only concerned about the alphabet, not any other character, so we separate out the list of all alphabets present in the sentence. We convert a list of alphabets into a set because the set contains unique values, if the length of the set is equal to the number of alphabets that means each alphabet occurred once then the sentence is a heterogram, otherwise not. Python3 # Function to Check whether a given string is Heterogram or not def heterogram(input): # separate out list of alphabets using list comprehension # ord function returns ascii value of character alphabets = [ch for ch in input if ( ord(ch) >= ord('a') and ord(ch) <= ord('z'))] # convert list of alphabets into set and # compare lengths if len(set(alphabets)) == len(alphabets): print('Yes') else: print('No') # Driver program if __name__ == "__main__": input = 'the big dwarf only jumps' heterogram(input) Output: YesCheck Whether a Given String is Heterogram using lower()+isalpha()In this approach, we sorts the input string in lowercase and then checks if any consecutive characters are equal and alphabetical. If such characters exist, then the function returns False, else it returns True, indicating that the input string is a heterogram. Python3 def is_heterogram(string): sorted_string = sorted(string.lower()) for i in range(1, len(sorted_string)): if sorted_string[i] == sorted_string[i-1] and sorted_string[i].isalpha(): return 'No' return 'Yes' string='the big dwarf only jumps' print(is_heterogram(string)) OutputYesTime Complexity: O(nlogn), where n is the length of the input string.Auxiliary Space: O(n), where n is the length of the input string. Comment More infoAdvertise with us Next Article Python Set | Check whether a given string is Heterogram or not S Shashank Mishra Follow Improve Article Tags : Python DSA python-set Practice Tags : pythonpython-set Similar Reads Check whether a given string is Heterogram or not Given a string S. The task is to check whether a the given string is Heterogram or not. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. Examples: Input : S = "the big dwarf only jumps" Output : Yes Each alphabet in the string S is occurred only o 4 min read Check if a given string is binary string or not - Python The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi 3 min read Python set to check if string is pangram 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 2 min read Check if given String is Pangram or not Given a string s, the task is to check if it is Pangram or not. A pangram is a sentence containing all letters of the English Alphabet.Examples: Input: s = "The quick brown fox jumps over the lazy dog" Output: trueExplanation: The input string contains all characters from âaâ to âzâ.Input: s = "The 6 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 res 3 min read Check if all given strings are isograms or not Given an array arr containing N strings, the task is to check if all strings are isogram or not. If they are, print Yes, otherwise No. An Isogram is a word in which no letter occurs more than once. Examples: Input: arr[] = {"abcd", "derg", "erty"}Output: Yes Input: arr[] = {"agka", "lkmn"}Output: No 4 min read Check if a String is Interleaving of Other Two Give three strings s1, s2 and s3, determine if s3 is formed by interleaving s1 and s2.A string s3 is an interleaving of s1 and s2 if:It contains all characters of s1 and s2 while preserving their relative order.Characters from s1 and s2 appear in s3 in the same order as in their original strings.The 15+ min read Check if a string is Isogram or not Given a word or phrase, check if it is an isogram or not. An Isogram is a word in which no letter occurs more than once Examples: Input: MachineOutput: TrueExplanation: "Machine" does not have any character repeating, it is an Isogram Input : GeekOutput : FalseExplanation: "Geek" has 'e' as repeatin 9 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â In 3 min read Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx 8 min read Like