Check Isogram in Java
Problem
An isogram is a word with no repeating letters — every letter it contains appears exactly once.
Given a word, determine whether it is an isogram.
Java Program
public class IsogramCheck {
public static void main(String[] args) {
String str = "background";
boolean[] seen = new boolean[26];
boolean isIsogram = true;
for (char c : str.toLowerCase().toCharArray()) {
if (c < 'a' || c > 'z') continue; // skip anything that isn't a letter
int index = c - 'a';
if (seen[index]) {
isIsogram = false;
break; // repeat found, no need to keep scanning
}
seen[index] = true;
}
System.out.println("Isogram: " + isIsogram);
}
}Output
Core Logic
Marking each letter as it's seen, and stopping the instant a letter shows up that's already marked, checks for repeats in a single pass.
- 1
str.toLowerCase()normalizes the word so uppercase and lowercase versions of the same letter are treated as a repeat. - 2A
boolean[26]array namedseenhas one slot per letter of the alphabet. - 3Non-letter characters are skipped with
continue, since only letters count toward the isogram check. - 4If a letter's slot in
seenis alreadytrue,isIsogramis set tofalseand the loop exits immediately withbreak.
"background", all ten letters are distinct, so every slot in seen is set exactly once and the scan finishes with isIsogram still true.Key Point: Breaking out as soon as a repeat is found avoids scanning the rest of the word once the answer is already known to be false.
Why: Each character is visited at most once, and the tracking array has a fixed size of 26 regardless of the word's length.
Key Concepts
Approach 2: Java 8
public class IsogramCheckStream {
public static void main(String[] args) {
String str = "background";
String lower = str.toLowerCase();
// Total letters vs. distinct letters — equal only if nothing repeats
long letterCount = lower.chars().filter(Character::isLetter).count();
// distinct() collapses repeats, so this only differs from letterCount if a letter repeats
long distinctCount = lower.chars().filter(Character::isLetter).distinct().count();
boolean isIsogram = letterCount == distinctCount;
System.out.println("Isogram: " + isIsogram);
}
}
Output
Core Logic
A word is an isogram exactly when its number of letters matches its number of distinct letters — no letter got counted twice.
- 1
lower.chars().filter(Character::isLetter)builds a stream of just the letter codes, dropping spaces or punctuation. - 2
.count()on that stream gives the total number of letters. - 3The same filtered stream, with
.distinct()added before.count(), gives the number of unique letters. - 4If the two counts are equal, no letter appeared more than once, so the word is an isogram.
"background", both the total letter count and the distinct letter count come out to 10, so isIsogram is true.Key Point: This runs the string through two separate stream pipelines to get both counts, so for very long words the single-pass boolean array is more efficient — a similar trade-off to the vowel/consonant counting program.
Why: distinct() has to track every letter it's already seen to filter out repeats, and the stream runs the word through twice — once for each count.