TheAlgorithms/Java · error · IllegalArgumentException

Symbol '{}' not found in the initial alphabet.

Error message

Symbol '{}' not found in the initial alphabet.

What it means

Thrown by MoveToFront.encode() when the input text contains a character that does not appear in the provided initialAlphabet string. The move-to-front transform requires every symbol in the text to exist in the alphabet; it performs a linear search (alphabet.indexOf(c)) and a -1 result triggers this error. This is a data-contract violation, not an internal logic failure.

Source

Thrown at src/main/java/com/thealgorithms/compression/MoveToFront.java:108

     */
    public static List<Integer> transform(String text, String initialAlphabet) {
        if (text == null || text.isEmpty()) {
            return new ArrayList<>();
        }
        if (initialAlphabet == null || initialAlphabet.isEmpty()) {
            throw new IllegalArgumentException("Alphabet cannot be null or empty when text is not empty.");
        }

        List<Integer> output = new ArrayList<>(text.length());

        // Use LinkedList for O(1) add-to-front and O(n) remove operations
        // This is more efficient than ArrayList for the move-to-front pattern
        List<Character> alphabet = initialAlphabet.chars().mapToObj(c -> (char) c).collect(Collectors.toCollection(LinkedList::new));

        for (char c : text.toCharArray()) {
            int index = alphabet.indexOf(c);
            if (index == -1) {
                throw new IllegalArgumentException("Symbol '" + c + "' not found in the initial alphabet.");
            }

            output.add(index);

            // Move the character to the front
            Character symbol = alphabet.remove(index);
            alphabet.addFirst(symbol);
        }
        return output;
    }

    /**
     * Performs the inverse Move-to-Front transform.
     * <p>
     * Reconstructs the original string from the list of indices produced by the
     * forward transform. This requires the exact same initial alphabet that was
     * used in the forward transform.
     * </p>

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Build the initialAlphabet to contain every distinct character in your text: new TreeSet<Character> from the text, then assemble the alphabet string.
  2. Normalize or filter the text before encoding (e.g., toUpperCase(), strip whitespace) to match the alphabet's character set.
  3. Pre-validate: iterate text.toCharArray() and check each char against initialAlphabet before calling encode().

Example fix

// before
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
List<Integer> out = MoveToFront.encode("Hello World", alphabet); // 'H' ok, 'e' not

// after
String text = "HELLOWORLD";
List<Integer> out = MoveToFront.encode(text.toUpperCase().replaceAll("[^A-Z]", ""), alphabet);
Defensive patterns

Strategy: validation

Validate before calling

String alphabet = initialAlphabet;
for (char c : text.toCharArray()) {
    if (alphabet.indexOf(c) == -1) {
        // c is not in the alphabet
        throw new IllegalArgumentException("char not in alphabet: " + c);
    }
}
List<Integer> out = MoveToFront.encode(text, alphabet);

Type guard

static boolean alphabetCoversText(String text, String alphabet) {
    for (char c : text.toCharArray()) {
        if (alphabet.indexOf(c) == -1) return false;
    }
    return true;
}

Try / catch

try {
    List<Integer> out = MoveToFront.encode(text, alphabet);
} catch (IllegalArgumentException e) {
    // message contains the offending character
    throw new DomainException("Cannot MTF-encode: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: Calling MoveToFront.encode(text, initialAlphabet) where text includes characters absent from initialAlphabet. Example: encoding lowercase text with an uppercase-only alphabet, or text containing whitespace/punctuation when the alphabet is purely alphanumeric.

Common situations: Using a default alphabet that omits characters present in real-world data (spaces, digits, punctuation, Unicode). Passing a lowercase alphabet with mixed-case input. Assuming the alphabet is case-insensitive when it is not.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/637678984d709731. Report an issue: GitHub.