Java ProgramsStringsCheck Isogram

Check Isogram in Java

intermediate·  Strings  ·  String

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.

Input
background
Output
Isogram: true

Java Program

Java
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

Isogram: true

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.

How It Works
  1. 1str.toLowerCase() normalizes the word so uppercase and lowercase versions of the same letter are treated as a repeat.
  2. 2A boolean[26] array named seen has one slot per letter of the alphabet.
  3. 3Non-letter characters are skipped with continue, since only letters count toward the isogram check.
  4. 4If a letter's slot in seen is already true, isIsogram is set to false and the loop exits immediately with break.
For "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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

boolean arraytoLowerCase()early exit with break

Approach 2: Java 8

Java
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

Isogram: true

Core Logic

A word is an isogram exactly when its number of letters matches its number of distinct letters — no letter got counted twice.

How It Works
  1. 1lower.chars().filter(Character::isLetter) builds a stream of just the letter codes, dropping spaces or punctuation.
  2. 2.count() on that stream gives the total number of letters.
  3. 3The same filtered stream, with .distinct() added before .count(), gives the number of unique letters.
  4. 4If the two counts are equal, no letter appeared more than once, so the word is an isogram.
For "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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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.

Key Concepts

Streamdistinct()count()

Related Programs