{"record":{"id":"59799e3beae41d27","repo":"TheAlgorithms/Java","slug":"index-is-out-of-bounds-for-the-current-alphabet","errorCode":null,"errorMessage":"Index {} is out of bounds for the current alphabet of size {}.","messagePattern":"Index (.+?) is out of bounds for the current alphabet of size (.+?)\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/MoveToFront.java","lineNumber":151,"sourceCode":"     * @param initialAlphabet the exact same initial alphabet string used for the forward transform;\n     *                        if {@code null} or empty, returns an empty string\n     * @return the original, untransformed string\n     * @throws IllegalArgumentException if any index in {@code indices} is negative or\n     *                                  exceeds the current alphabet size\n     */\n    public static String inverseTransform(Collection<Integer> indices, String initialAlphabet) {\n        if (indices == null || indices.isEmpty() || initialAlphabet == null || initialAlphabet.isEmpty()) {\n            return \"\";\n        }\n\n        StringBuilder output = new StringBuilder(indices.size());\n\n        // Use LinkedList for O(1) add-to-front and O(n) remove operations\n        List<Character> alphabet = initialAlphabet.chars().mapToObj(c -> (char) c).collect(Collectors.toCollection(LinkedList::new));\n\n        for (int index : indices) {\n            if (index < 0 || index >= alphabet.size()) {\n                throw new IllegalArgumentException(\"Index \" + index + \" is out of bounds for the current alphabet of size \" + alphabet.size() + \".\");\n            }\n\n            // Get the symbol at the index\n            char symbol = alphabet.get(index);\n            output.append(symbol);\n\n            // Move the symbol to the front (mirroring the forward transform)\n            alphabet.remove(index);\n            alphabet.addFirst(symbol);\n        }\n        return output.toString();\n    }\n}\n","sourceCodeStart":133,"sourceCodeEnd":165,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/MoveToFront.java#L133-L165","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the exact same initialAlphabet string for both encode() and inverseTransform().","Verify every index in the collection is in range [0, initialAlphabet.length()-1] before calling inverseTransform().","Persist the alphabet alongside the encoded indices so decode always reconstructs with the same one."],"exampleFix":"// before\nString encAlpha = \"ABCD...\";\nList<Integer> idx = MoveToFront.encode(text, encAlpha);\nString out = MoveToFront.inverseTransform(idx, \"ABCD\"); // wrong alphabet length\n\n// after\nString encAlpha = \"ABCD...\";\nList<Integer> idx = MoveToFront.encode(text, encAlpha);\nString out = MoveToFront.inverseTransform(idx, encAlpha); // identical alphabet","handlingStrategy":"validation","validationCode":"int maxIndex = initialAlphabet.length() - 1;\nfor (int idx : indices) {\n    if (idx < 0 || idx > maxIndex) {\n        throw new IllegalArgumentException(\"index out of alphabet range: \" + idx);\n    }\n}\nString out = MoveToFront.inverseTransform(indices, initialAlphabet);","typeGuard":"static boolean indicesFitAlphabet(Collection<Integer> indices, String alphabet) {\n    int max = alphabet.length() - 1;\n    for (int i : indices) if (i < 0 || i > max) return false;\n    return true;\n}","tryCatchPattern":"try {\n    String out = MoveToFront.inverseTransform(indices, alphabet);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"MTF decode failed: \" + e.getMessage(), e);\n}","preventionTips":["Use the identical alphabet string for encode and inverseTransform.","Store the alphabet with the encoded indices, not separately.","Validate all indices are within [0, alphabet.length()-1] before decoding."],"tags":["compression","move-to-front","inverse-transform","bounds","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}