{"record":{"id":"6bd793015e3343ec","repo":"TheAlgorithms/Java","slug":"original-index-must-be-between-0-and-got","errorCode":null,"errorMessage":"Original index must be between 0 and {}, got: {}","messagePattern":"Original index must be between 0 and (.+?), got: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/BurrowsWheelerTransform.java","lineNumber":178,"sourceCode":"     *   <li>Following this mapping starting from the original index to reconstruct the string</li>\n     * </ol>\n     * </p>\n     *\n     * @param bwtString the transformed string (L-column) from the forward transform; must not be {@code null}\n     * @param originalIndex the index of the original string row from the forward transform;\n     *                      use -1 for empty strings\n     * @return the original, untransformed string; returns empty string if input is empty or {@code originalIndex} is -1\n     * @throws NullPointerException if {@code bwtString} is {@code null}\n     * @throws IllegalArgumentException if {@code originalIndex} is out of valid range (except -1)\n     */\n    public static String inverseTransform(String bwtString, int originalIndex) {\n        if (bwtString == null || bwtString.isEmpty() || originalIndex == -1) {\n            return \"\";\n        }\n\n        int n = bwtString.length();\n        if (originalIndex < 0 || originalIndex >= n) {\n            throw new IllegalArgumentException(\"Original index must be between 0 and \" + (n - 1) + \", got: \" + originalIndex);\n        }\n\n        char[] lastColumn = bwtString.toCharArray();\n        char[] firstColumn = bwtString.toCharArray();\n        Arrays.sort(firstColumn);\n\n        // Create the \"next\" array for LF-mapping.\n        // next[i] stores the row index in the last column that corresponds to firstColumn[i]\n        int[] next = new int[n];\n\n        // Track the count of each character seen so far in the last column\n        Map<Character, Integer> countMap = new HashMap<>();\n\n        // Store the first occurrence index of each character in the first column\n        Map<Character, Integer> firstOccurrence = new HashMap<>();\n\n        for (int i = 0; i < n; i++) {\n            if (!firstOccurrence.containsKey(firstColumn[i])) {","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/BurrowsWheelerTransform.java#L160-L196","documentation":"BurrowsWheelerTransform.inverseTransform(String, int) reverses the transform using the original row index, which must point at a valid row in the n x n rotation table — i.e. an index in [0, n-1]. An index outside that range cannot locate the correct starting row, so the method rejects it with the valid bounds echoed in the message.","triggerScenarios":"Calling inverseTransform(bwt, -2) (negative other than the sentinel -1); inverseTransform(\"abc\", 5) (index >= length 3); inverseTransform with an index produced by a mismatched transform.","commonSituations":"The originalIndex stored alongside the BWT was corrupted or came from a different transform; an off-by-one when persisting/reading the index; index default-initialized to a value like Integer.MAX_VALUE.","solutions":["Pass the exact originalIndex returned by the forward transform — it must satisfy 0 <= index < bwtString.length().","Store the index together with the BWT string so the pair cannot get out of sync.","Use -1 as the sentinel for empty strings (it returns \"\" instead of throwing)."],"exampleFix":"// before\nString original = BurrowsWheelerTransform.inverseTransform(bwt, storedIndex);\n\n// after\nif (storedIndex < 0 || storedIndex >= bwt.length()) {\n    throw new IllegalArgumentException(\"Index \" + storedIndex + \" out of range for BWT of length \" + bwt.length());\n}\nString original = BurrowsWheelerTransform.inverseTransform(bwt, storedIndex);","handlingStrategy":"validation","validationCode":"if (originalIndex < 0 || originalIndex >= bwtString.length()) {\n    throw new IllegalArgumentException(\"originalIndex \" + originalIndex + \" out of bounds [0,\" + (bwtString.length() - 1) + \"]\");\n}\nString original = BurrowsWheelerTransform.inverseTransform(bwtString, originalIndex);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Store the index returned by the forward transform alongside the BWT string.","Use -1 as the sentinel for empty strings.","Validate the index against the BWT length before inverting."],"tags":["compression","bwt","index","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}