{"record":{"id":"637678984d709731","repo":"TheAlgorithms/Java","slug":"symbol-not-found-in-the-initial-alphabet","errorCode":null,"errorMessage":"Symbol '{}' not found in the initial alphabet.","messagePattern":"Symbol '(.+?)' not found in the initial alphabet\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/MoveToFront.java","lineNumber":108,"sourceCode":"     */\n    public static List<Integer> transform(String text, String initialAlphabet) {\n        if (text == null || text.isEmpty()) {\n            return new ArrayList<>();\n        }\n        if (initialAlphabet == null || initialAlphabet.isEmpty()) {\n            throw new IllegalArgumentException(\"Alphabet cannot be null or empty when text is not empty.\");\n        }\n\n        List<Integer> output = new ArrayList<>(text.length());\n\n        // Use LinkedList for O(1) add-to-front and O(n) remove operations\n        // This is more efficient than ArrayList for the move-to-front pattern\n        List<Character> alphabet = initialAlphabet.chars().mapToObj(c -> (char) c).collect(Collectors.toCollection(LinkedList::new));\n\n        for (char c : text.toCharArray()) {\n            int index = alphabet.indexOf(c);\n            if (index == -1) {\n                throw new IllegalArgumentException(\"Symbol '\" + c + \"' not found in the initial alphabet.\");\n            }\n\n            output.add(index);\n\n            // Move the character to the front\n            Character symbol = alphabet.remove(index);\n            alphabet.addFirst(symbol);\n        }\n        return output;\n    }\n\n    /**\n     * Performs the inverse Move-to-Front transform.\n     * <p>\n     * Reconstructs the original string from the list of indices produced by the\n     * forward transform. This requires the exact same initial alphabet that was\n     * used in the forward transform.\n     * </p>","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/MoveToFront.java#L90-L126","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Build the initialAlphabet to contain every distinct character in your text: new TreeSet<Character> from the text, then assemble the alphabet string.","Normalize or filter the text before encoding (e.g., toUpperCase(), strip whitespace) to match the alphabet's character set.","Pre-validate: iterate text.toCharArray() and check each char against initialAlphabet before calling encode()."],"exampleFix":"// before\nString alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\nList<Integer> out = MoveToFront.encode(\"Hello World\", alphabet); // 'H' ok, 'e' not\n\n// after\nString text = \"HELLOWORLD\";\nList<Integer> out = MoveToFront.encode(text.toUpperCase().replaceAll(\"[^A-Z]\", \"\"), alphabet);","handlingStrategy":"validation","validationCode":"String alphabet = initialAlphabet;\nfor (char c : text.toCharArray()) {\n    if (alphabet.indexOf(c) == -1) {\n        // c is not in the alphabet\n        throw new IllegalArgumentException(\"char not in alphabet: \" + c);\n    }\n}\nList<Integer> out = MoveToFront.encode(text, alphabet);","typeGuard":"static boolean alphabetCoversText(String text, String alphabet) {\n    for (char c : text.toCharArray()) {\n        if (alphabet.indexOf(c) == -1) return false;\n    }\n    return true;\n}","tryCatchPattern":"try {\n    List<Integer> out = MoveToFront.encode(text, alphabet);\n} catch (IllegalArgumentException e) {\n    // message contains the offending character\n    throw new DomainException(\"Cannot MTF-encode: \" + e.getMessage(), e);\n}","preventionTips":["Derive the alphabet from the text itself when possible: collect distinct chars first.","Normalize text (case, whitespace) before encoding to match a known alphabet.","Persist the alphabet alongside encoded data so encode/decode stay consistent."],"tags":["compression","move-to-front","alphabet","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}