Check Isogram String or Not

Check Isogram String or Not” is a basic problem of string data structure. Here, we are given a string of alphabets only and our task is to write a program to check whether the string is isogram or not. 

An isogram string is a string in which every character occurs only once.

Example:

INPUT:
ABCDER
OUTPUT:
ABCDER is an Isogram String

INPUT:
ABCDC
OUTPUT:
ABCDC is not an Isogram String

There are various methods to check if string is isogram string or not. Some of them are:

METHOD 1: Brute-Force Approach to Check Isogram String or Not

The simplest solution would be to check the complete string for every character of the string. if there is any character with duplicate presence, return false. Else, return true.

The time complexity of this solution is O(n^2).

C++ Program to Check Isogram String or Not is as follows:

/* C++ Program to Check Isogram String or Not */
#include<bits/stdc++.h>  
using namespace std;  
void isIsogram (string str)
{
    for(int i = 0 ; i<str.length() ; i++)
    {
        for(int j = i+1 ; j <str.length() ; j++)
        {
            if(str[i] == str[j])
            {
                cout<<"The string is not isogram\n";
                return;
            }
        }
    }
    cout<<"The string is isogram\n";
}
int main()  
{
    string str;
    cout<<"Enter a String:\n";
    cin>>str;
    /* Check Isogram String */
    isIsogram(str);
}

OUTPUT:
Enter a String:
Helpmestudybro
The string is not isogramic

METHOD 2: Hashing Based Approach to Check Isogram String or Not

We know that the string contains alphabets only, it could be lower-case or upper-case. The ascii value of these alphabets ranges from 65-90 and 97-122. So, maximum number is 122, we initialize an integer array with size 123 and value as 0. 

Then we traverse string and increment the value of array for every character of the string. While incrementing, we check if at any time the counter for that alphabet reaches 2, if yes, then, we will return false.

After complete traversal, if the counter of any character does not reaches 2, then simply return true.

The time complexity of this solution is O(n).

C++ Program to Check Isogram String or Not is as follows:

/* Program to Check Isogram String or Not */
#include<bits/stdc++.h>  
using namespace std;
void isIsogram(string str)
{
    int hash[123]={0};
    for(int i=0;i<str.length();i++)
    {
        hash[str[i]]++;
        if(hash[str[i]] == 2)
        {
            cout<<"The string is not isogram\n";
             return;
        }
    }
    cout<<"The string is isogram\n";
}
int main()  
{
    string str;
    cout<<"Enter a String:\n";
    cin>>str;
    /* Check Isogram */
    isIsogram(str);
}

OUTPUT:
 Enter a String:
Helpmestudybro
The string is not isogramic

Related Posts:

  1. Program to Reverse Each word of the String.
  2. Program to Print All the Substring of the Given String.
  3. Program to find longest palindromic Substring.
  4. Program to check Anagram Strings.
  5. Program to check whether two Strings are Rotation of each other or not.
  6. Program to check Palindromic Anagram.
  7. Program to print all the Palindromic Substring of the String.
  8. Program to check Panagram String.
  9. Program to find first non-repeating character of the String.
  10. Program to Reverse a String.
  11. Program to Count Number of Characters in a String.
  12. Program to Count frequency of particular character of the String.
  13. Program to Count Number of Vowels and Number of Consonants in a String.
  14. Program to Check if String is alphabetic or numeric or alpha-numeric.
  15. Program to copy one String to Another.
  16. Program to Concatenate Two Strings.
  17. Program to Check whether given String is Palindrome or not.
  18. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  19. Program to count total number of words in a string.
  20. Print Bracket Number

You may also like...