1. Using indexOf() and lastIndexOf() methods
2. Using HashSet
3. Using Java8
Read Also: Find the first non-repeated character in a String
Before moving on to the solution let's first understand the term isogram.
What is an Isogram?
In simple words, an isogram is a word that has no repeating letters, consecutive or non-consecutive. In other words, a word without repeating letters.
Examples:
"Dermatoglyphics" is an Isogram because it does not have any repeating letters.
"aba" NOT an Isogram because the letter 'a' is repeating.
"moOse" NOT an Isogram because the letter 'o' is repeating.
Isogram Checker Java Program
1. Using indexOf() and lastIndexOf() methods
For a given character, if indexOf() and lastIndexOf() methods return the same value, it means there are no repeating characters in the String. public class IsIsogram {
public static boolean checkWordIsAnIsogram(String word) {
if(word == null)
return false;
// Converting all letters of a word to lowercase
word = word.toLowerCase();
// Convert String to char[]
char[] arr = word.toCharArray();
//Iterating the char array
for (char ch : arr) {
/* if positions returned by the indexOf() and lastIndexOf() methods are not same
then the word is NOT Isogram*/
if(word.indexOf(ch) != word.lastIndexOf(ch)) {
return false;
}
}
return true;
}
public static void main(String args[]) {
String givenWord = "Dermatoglyphics";
boolean result = checkWordIsAnIsogram(givenWord);
if(result == true) {
System.out.println(givenWord + " is an Isogram");
}
else {
System.out.println(givenWord + " NOT an Isogram");
}
}
}
Output:
Dermatoglyphics is an Isogram
2. Using HashSet
We will add all the characters of the given word to the HashSet. If the size of HashSet is equal to the length of the given word, then the given word is an Isogram otherwise not.
import java.util.HashSet;
public class IsIsogramTwo {
public static boolean checkWordIsAnIsogram(String word) {
HashSet<Character> set = new HashSet<>();
// Converting all letters of a word to lowercase
word = word.toLowerCase();
// Convert String to char[]
char[] arr = word.toCharArray();
//Iterating the char array
for (char ch : arr) {
/* adding the characters to the set*/
set.add(ch);
}
if (set.size() == word.length())
return true;
else
return false;
}
public static void main(String args[]) {
String givenWord = "moOse";
boolean result = checkWordIsAnIsogram(givenWord);
if(result == true) {
System.out.println(givenWord + " is an Isogram");
}
else {
System.out.println(givenWord + " NOT an Isogram");
}
}
}
Output:
moOse NOT an Isogram
3. Using Java8
We can use Java8 to determine if a word is an isogram. Just compare the length of the word to the number of distinct characters present in the word. If both are the same then the given word is an isogram otherwise not. public class IsIsogramThree {
public static boolean checkWordIsAnIsogram(String word) {
return word.length() == word.toLowerCase().chars().distinct().count();
}
public static void main(String args[]) {
String givenWord = "aba";
boolean result = checkWordIsAnIsogram(givenWord);
if(result == true) {
System.out.println(givenWord + " is an Isogram");
}
else {
System.out.println(givenWord + " NOT an Isogram");
}
}
}
Output:
aba NOT an Isogram
That's all for today, please mention in the comments in case you know any other way of solving isogram in Java.