Open In App

Python Set | Check whether a given string is Heterogram or not

Last Updated : 01 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S of lowercase characters. The task is to check whether a given string is a Heterogram or not using Python. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once.

Input1: S = "the big dwarf only jumps"
Output1: Yes
Explanation: Each alphabet in the string S is occurred only once.

Input2: S = "geeksforgeeks"
Output2: No
Explanation: Since alphabet 'g', 'e', 'k', 's' occurred more than once.

We have an existing solution for this problem. Please refer to Check whether a given string is a Heterogram or not link. Some other solutions for this problem are given below.

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

Below are the approaches using which we can check whether a given string is Heterogram or not:

Check Whether a Given String is Heterogram using Set

The approach is to check sentence is a heterogram or not. We are only concerned about the alphabet, not any other character, so we separate out the list of all alphabets present in the sentence. We convert a list of alphabets into a set because the set contains unique values, if the length of the set is equal to the number of alphabets that means each alphabet occurred once then the sentence is a heterogram, otherwise not.

Python3




# Function to Check whether a given string is Heterogram or not
def heterogram(input):
    # separate out list of alphabets using list comprehension
    # ord function returns ascii value of character
    alphabets = [ch for ch in input if (
        ord(ch) >= ord('a') and ord(ch) <= ord('z'))]
    # convert list of alphabets into 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)


Output:

Yes

Check Whether a Given String is Heterogram using lower()+isalpha()

In this approach, we sorts the input string in lowercase and then checks if any consecutive characters are equal and alphabetical. If such characters exist, then the function returns False, else it returns True, indicating that the input string is a heterogram. 

Python3




def is_heterogram(string):
    sorted_string = sorted(string.lower())
    for i in range(1, len(sorted_string)):
        if sorted_string[i] == sorted_string[i-1] and sorted_string[i].isalpha():
            return 'No'
    return 'Yes'
string='the big dwarf only jumps'
print(is_heterogram(string))


Output

Yes

Time Complexity: O(nlogn), where n is the length of the input string.
Auxiliary Space: O(n), where n is the length of the input string.



Similar Reads

Python3 Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Python | Check whether string contains only numbers or not
Given a string, write a Python program to find whether a string contains only numbers or not. Let's see a few methods to solve the above task. Check if String Contains Only Numbers Check if String Contains Only Numbers using isdigit() method Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It retu
4 min read
Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
Given a string. The task is to write a regular expression to check whether a string starts and ends with the same character. Examples: Input : abbaOutput : ValidInput : aOutput : ValidInput : abcOutput : InvalidSolution: The input can be divide into 2 cases: Single character string: All single character strings satisfies the condition that they sta
2 min read
Python Program to check whether Characters of all string elements are in lexical order or not
Given a list with string elements, the task is to write a Python program to return True if all list elements are sorted. The point of caution here is to keep in mind we are comparing characters of a string list element with other characters of the same list element and not string elements with each other. Examples: Input : test_list = ["dent", "flo
4 min read
Python | Check whether a string is valid json or not
Given a Python String, the task is to check whether the String is a valid JSON object or not. Let's try to understand the problem using different examples in Python. Validate JSON String in PythonThere are various methods to Validate JSON schema in Python here we explain some generally used methods for validating JSON schema in Python which are the
3 min read
Python - Check whether the given List forms Contiguous Distinct Sub-Array or Not
You are given an array consisting of elements in the form A1, A2, A3.......An. The task is to find whether the array can be formed as a Contiguous Distinct Sub Array or Not. You need to find whether the array can be converted to contiguous sub-arrays that consist of similar elements and there are a distinct number of each element. The elements once
6 min read
Check whether the Average Character of the String is present or not
Given a string of alphanumeric characters, the task is to check whether the average character of the string is present or not. Average character refers to the character corresponding to the ASCII value which is the floor of the average value of the ASCII values of all characters in the string. Examples: Input: abcdefOutput: d YesExplanation: string
7 min read
Check whether a given column is present in a Pandas DataFrame or not
Consider a Dataframe with 4 columns : 'ConsumerId', 'CarName', CompanyName, and 'Price'. We have to determine whether a particular column is present in the DataFrame or not in Pandas Dataframe using Python. Creating a Dataframe to check if a column exists in Dataframe C/C++ Code # import pandas library import pandas as pd # dictionary d = {'Consume
2 min read
Python regex | Check whether the input is Floating point number or not
Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is Floating point number or not. Examples: Input: 1.20 Output: Floating point number Input: -2.356 Output: Floating point number Input: 0.2 Output: Floating point number Input: -3 Output: Not a Floating point number In this program, we
2 min read
Python | Check whether two lists follow same pattern or not
Given two lists [Tex]A[/Tex] and [Tex]B[/Tex], check if they follow the same pattern or not. Condition for pattern matching: [Tex]Ai[/Tex] &gt; [Tex]Aj[/Tex], then [Tex]Bi[/Tex] &gt; [Tex]Bj[/Tex] [Tex]Ai[/Tex] = [Tex]Aj[/Tex], then [Tex]Bi[/Tex] = [Tex]Bj[/Tex] [Tex]Ai[/Tex] &lt; [Tex]Aj[/Tex], then [Tex]Bi[/Tex] &lt; [Tex]Bj[/Tex], for all i, j.
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg