Python program to check whether a given string is Heterogram or not

PythonProgrammingServer Side Programming

Here one string is given then our task is to check weather a given string is Heterogram or not.

The meaning of heterogram checking is that a word, phrase, or sentence in which no letter of the alphabet occurs more than once. A heterogram may be distinguished from a pangram which uses all of the letters of the alphabet.


String is abc def ghi

This is Heterogram (no alphabet repeated)

String is abc bcd dfh

This is not Heterogram. (b,c,d are repeated)


Step 1: first we separate out list of all alphabets present in sentence.
Step 2: Convert list of alphabets into set because set contains unique values.
Step 3: if length of set is equal to number of alphabets that means each alphabet occurred once then sentence is heterogram, otherwise not.

Example Code

def stringheterogram(s, n):
   hash = [0] * 26
   for i in range(n):
   if s[i] != ' ':
      if hash[ord(s[i]) - ord('a')] == 0:
      hash[ord(s[i]) - ord('a')] = 1
   return False
   return True

   # Driven Code
   s = input("Enter the String ::>")
   n = len(s)
print(s,"This string is Heterogram" if stringheterogram(s, n) else "This string is not Heterogram")


Enter the String ::> asd fgh jkl
asd fgh jkl this string is Heterogram

Enter the String ::>asdf asryy
asdf asryy This string is not Heterogram
Published on 26-Sep-2018 12:27:30