Change language

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



 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)" = ord ( ’a’ ) and ord (ch) " = 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 )




Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method