VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-string-isogram-not/

⇱ Check if a string is Isogram or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a string is Isogram or not

Last Updated : 19 Sep, 2022

Given a word or phrase, check if it is an isogram or not. An Isogram is a word in which no letter occurs more than once

Examples: 

Input: Machine
Output: True
Explanation: "Machine" does not have any character repeating, it is an Isogram

Input : Geek
Output : False
Explanation: "Geek" has 'e' as repeating character, it is not an Isogram

To solve the problem follow the below idea:

Sort the string and for every character check, if the current character is equal to the previous character or not. If it is equal then the string is not an isogram

Follow the given steps to solve the problem:

  • Convert the string into lowercase English letters
  • Sort the string
  • Traverse the string and check for every character
    • If the current character is equal to the character on the previous index then return false
  • Return true

Below is the implementation of the above approach:


Output
True
True
False
False

Time Complexity: O(N log N)
Auxiliary Space: O(1)

To solve the problem follow the below idea:

In this, the count of characters of the string is stored in the hashmap, and wherever it is found to be greater than 1 for any char, return false else return true at the end

Follow the given steps to solve the problem:

  • Declare a hashmap to store the count of the characters of the string
  • Traverse the string
    • Increase the count of the current character in the hashmap
    • If the count of the current character is greater than one then return false
  • Return true

Below is the implementation of the above approach:


Output
False
True

Time Complexity: O(N)
Auxiliary Space: O(1), as an array of fixed size 26 is used.

Thanks Sahil Bansal for suggesting the above method. 
If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.

Comment
Article Tags:
Article Tags: