# 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: