Set of pythons | Check if the given string is heterogram or not

Python Methods and Functions | String Variables


 Input: S = "the big dwarf only jumps" Output: Yes Each alphabet in the string S is occurred only once. Input: S = "pythonengineering" Output: No Since alphabet 'g',' e', 'k',' s' occurred more than once. 

We have a solution to this problem. See Set data structure . The approach is very simple,

  1. To check the offer — whether it is a heterogram or not, we are only interested in the alphabets and not any other character, so we highlight a list of all alphabets present in the sentence.
  2. Convert the list of alphabets to a set because the set contains unique values ​​if the length of the set is equal to the number of alphabets, which means that each alphabet occurs once, then the sentence is a heterogram, otherwise — no.

# Function to check if a given string is a heterogram or not


def heterogram ( input ):


# highlight a list of alphabets using a list comprehension

  The # ord function returns the ascii character value

alphabets = [ch for ch in input if ( ord (ch) & gt; = ord ( 'a' ) and ord (ch) & lt; = ord ( 'z' ))]


# convert a list of alphabets to a 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 )