Python Set | Check whether a given string is Heterogram or not
Given a string S of lower case characters. 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.
Input : S = "the big dwarf only jumps" Output : Yes Each alphabet in the string S is occurred only once. Input : S = "geeksforgeeks" Output : No Since alphabet 'g', 'e', 'k', 's' occurred more than once.
We have existing solution for this problem please refer Check whether a given string is Heterogram or not link. We can solve this problem quickly in python using Set data structure. Approach is very simple,
- To check sentence is heterogram or not we only concern about alphabets not any other character, so we separate out list of all alphabets present in sentence.
- Convert list of alphabets into set because set contains unique values, if length of set is equal to number of alphabets that means each alphabet occured once then sentence is heterogram, otherwise not.