TheAlgorithms/Java · error · IllegalArgumentException

Index is out of bounds for the current alphabet of size .

Error message

Index {} is out of bounds for the current alphabet of size {}.

What it means

Thrown by MoveToFront.inverseTransform() when an index in the indices collection is negative or greater-than/equal to the current alphabet size. Because the alphabet shrinks/grows dynamically during the inverse transform (remove + addFirst), the valid range depends on the alphabet size at the point each index is processed, not the original size.

Solutions

  1. Use the exact same initialAlphabet string for both encode() and inverseTransform().
  2. Verify every index in the collection is in range [0, initialAlphabet.length()-1] before calling inverseTransform().
  3. Persist the alphabet alongside the encoded indices so decode always reconstructs with the same one.

Example fix

// before
String encAlpha = "ABCD...";
List<Integer> idx = MoveToFront.encode(text, encAlpha);
String out = MoveToFront.inverseTransform(idx, "ABCD"); // wrong alphabet length

// after
String encAlpha = "ABCD...";
List<Integer> idx = MoveToFront.encode(text, encAlpha);
String out = MoveToFront.inverseTransform(idx, encAlpha); // identical alphabet
Defensive patterns

Strategy: validation

Validate before calling

int maxIndex = initialAlphabet.length() - 1;
for (int idx : indices) {
    if (idx < 0 || idx > maxIndex) {
        throw new IllegalArgumentException("index out of alphabet range: " + idx);
    }
}
String out = MoveToFront.inverseTransform(indices, initialAlphabet);

Type guard

static boolean indicesFitAlphabet(Collection<Integer> indices, String alphabet) {
    int max = alphabet.length() - 1;
    for (int i : indices) if (i < 0 || i > max) return false;
    return true;
}

Try / catch

try {
    String out = MoveToFront.inverseTransform(indices, alphabet);
} catch (IllegalArgumentException e) {
    throw new DomainException("MTF decode failed: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: Passing indices produced by a different alphabet than the one supplied to inverseTransform(). Passing corrupted/truncated indices. Supplying indices from encode() with an alphabet whose length differs from the one used at encode time.

Common situations: Storing indices but not the alphabet, then reconstructing with a different alphabet. Manually editing or transmitting the integer list and introducing off-by-one or sign errors. Mixing up the alphabet ordering between encode and decode.

Related errors


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

Appendix: source

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

     * @param initialAlphabet the exact same initial alphabet string used for the forward transform;
     *                        if {@code null} or empty, returns an empty string
     * @return the original, untransformed string
     * @throws IllegalArgumentException if any index in {@code indices} is negative or
     *                                  exceeds the current alphabet size
     */
    public static String inverseTransform(Collection<Integer> indices, String initialAlphabet) {
        if (indices == null || indices.isEmpty() || initialAlphabet == null || initialAlphabet.isEmpty()) {
            return "";
        }

        StringBuilder output = new StringBuilder(indices.size());

        // Use LinkedList for O(1) add-to-front and O(n) remove operations
        List<Character> alphabet = initialAlphabet.chars().mapToObj(c -> (char) c).collect(Collectors.toCollection(LinkedList::new));

        for (int index : indices) {
            if (index < 0 || index >= alphabet.size()) {
                throw new IllegalArgumentException("Index " + index + " is out of bounds for the current alphabet of size " + alphabet.size() + ".");
            }

            // Get the symbol at the index
            char symbol = alphabet.get(index);
            output.append(symbol);

            // Move the symbol to the front (mirroring the forward transform)
            alphabet.remove(index);
            alphabet.addFirst(symbol);
        }
        return output.toString();
    }
}

View on GitHub (pinned to fdfb9a395b)