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
Check if a string is Isogram or not using sorting:
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:
C++
#include <bits/stdc++.h>
using namespace std;
string is_isogram(string str)
{
int len = str.length();
for ( int i = 0; i < len; i++)
str[i] = tolower (str[i]);
sort(str.begin(), str.end());
for ( int i = 0; i < len; i++) {
if (str[i] == str[i + 1])
return "False" ;
}
return "True" ;
}
int main()
{
string str1 = "Machine" ;
cout << is_isogram(str1) << endl;
string str2 = "isogram" ;
cout << is_isogram(str2) << endl;
string str3 = "GeeksforGeeks" ;
cout << is_isogram(str3) << endl;
string str4 = "Alphabet" ;
cout << is_isogram(str4) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static boolean is_isogram(String str)
{
str = str.toLowerCase();
int len = str.length();
char arr[] = str.toCharArray();
Arrays.sort(arr);
for ( int i = 0 ; i < len - 1 ; i++) {
if (arr[i] == arr[i + 1 ])
return false ;
}
return true ;
}
public static void main(String[] args)
{
String str1 = "Machine" ;
System.out.println(is_isogram(str1));
String str2 = "isogram" ;
System.out.println(is_isogram(str2));
String str3 = "GeeksforGeeks" ;
System.out.println(is_isogram(str3));
String str4 = "Alphabet" ;
System.out.println(is_isogram(str4));
}
}
|
Python3
def is_isogram(word):
clean_word = word.lower()
letter_list = []
for letter in clean_word:
if letter.isalpha():
if letter in letter_list:
return False
letter_list.append(letter)
return True
if __name__ = = '__main__' :
print (is_isogram( "Machine" ))
print (is_isogram( "isogram" ))
print (is_isogram( "GeeksforGeeks" ))
print (is_isogram( "Alphabet " ))
|
C#
using System;
public class GFG {
static bool is_isogram( string str)
{
str = str.ToLower();
int len = str.Length;
char [] arr = str.ToCharArray();
Array.Sort(arr);
for ( int i = 0; i < len - 1; i++) {
if (arr[i] == arr[i + 1])
return false ;
}
return true ;
}
public static void Main()
{
string str1 = "Machine" ;
Console.WriteLine(is_isogram(str1));
string str2 = "isogram" ;
Console.WriteLine(is_isogram(str2));
string str3 = "GeeksforGeeks" ;
Console.WriteLine(is_isogram(str3));
string str4 = "Alphabet" ;
Console.WriteLine(is_isogram(str4));
}
}
|
Javascript
<script>
function is_isogram(str)
{
str = str.toLowerCase();
let len = str.length;
let arr = str.split( '' );
arr.sort();
for (let i = 0; i < len - 1; i++) {
if (arr[i] == arr[i + 1])
return false ;
}
return true ;
}
let str1 = "Machine" ;
if (is_isogram(str1))
{
document.write( "True" + "</br>" );
}
else {
document.write( "False" + "</br>" );
}
let str2 = "isogram" ;
if (is_isogram(str2))
{
document.write( "True" + "</br>" );
}
else {
document.write( "False" + "</br>" );
}
let str3 = "GeeksforGeeks" ;
if (is_isogram(str3))
{
document.write( "True" + "</br>" );
}
else {
document.write( "False" + "</br>" );
}
let str4 = "Alphabet" ;
if (is_isogram(str4))
{
document.write( "True" + "</br>" );
}
else {
document.write( "False" + "</br>" );
}
</script>
|
Output
True
True
False
False
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Check if a string is Isogram or not using Hash-Map:
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:
C++
#include <bits/stdc++.h>
using namespace std;
bool check_isogram(string str)
{
int length = str.length();
int mapHash[26] = { 0 };
for ( int i = 0; i < length; i++) {
mapHash[str[i] - 'a' ]++;
if (mapHash[str[i] - 'a' ] > 1) {
return false ;
}
}
return true ;
}
int main()
{
string str = "geeks" ;
string str2 = "computer" ;
if (check_isogram(str)) {
cout << "True" << endl;
}
else {
cout << "False" << endl;
}
if (check_isogram(str2)) {
cout << "True" << endl;
}
else {
cout << "False" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean check_isogram(String str)
{
int length = str.length();
int mapHash[] = new int [ 26 ];
for ( int i = 0 ; i < length; i++) {
mapHash[str.charAt(i) - 'a' ]++;
if (mapHash[str.charAt(i) - 'a' ] > 1 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String str = "geeks" ;
String str2 = "computer" ;
if (check_isogram(str))
System.out.println( "True" );
else
System.out.println( "False" );
if (check_isogram(str2))
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python3
def check_isogram(string):
length = len (string)
mapHash = [ 0 ] * 26
for i in range (length):
mapHash[ ord (string[i]) - ord ( 'a' )] + = 1
if (mapHash[ ord (string[i]) - ord ( 'a' )] > 1 ):
return False
return True
if __name__ = = "__main__" :
string = "geeks"
string2 = "computer"
if (check_isogram(string)):
print ( "True" )
else :
print ( "False" )
if (check_isogram(string2)):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
public class GFG {
static bool check_isogram(String str)
{
int length = str.Length;
int [] mapHash = new int [26];
for ( int i = 0; i < length; i++) {
mapHash[str[i] - 'a' ]++;
if (mapHash[str[i] - 'a' ] > 1) {
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String str = "geeks" ;
String str2 = "computer" ;
if (check_isogram(str))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
if (check_isogram(str2))
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
Javascript
<script>
function check_isogram(str)
{
let length = str.length;
let mapHash = new Array(26);
mapHash.fill(0);
for (let i = 0; i < length; i++)
{
mapHash[str[i].charCodeAt() - 'a' .charCodeAt()]++;
if (mapHash[str[i].charCodeAt() - 'a' .charCodeAt()] > 1)
{
return false ;
}
}
return true ;
}
let str = "geeks" ;
let str2 = "computer" ;
if (check_isogram(str))
document.write( "True" + "</br>" );
else
document.write( "False" + "</br>" );
if (check_isogram(str2))
document.write( "True" + "</br>" );
else
document.write( "False" + "</br>" );
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...