
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Given String is Heterogram in Python
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.
Example
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)
Algorithm
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 else: 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")
Output
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
Advertisements