{"record":{"id":"1abd914531110d89","repo":"doocs/leetcode","slug":"invalid-char","errorCode":null,"errorMessage":"Invalid char: ","messagePattern":"Invalid char: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"solution/0400-0499/0488.Zuma Game/Solution.java","lineNumber":160,"sourceCode":"        }\n\n        long stateBits = 0;\n        for (char ch : stateChars) {\n            stateBits = (stateBits << 3) | Zuma.encode(ch);\n        }\n        return stateBits;\n    }\n\n    private static long encode(char ch) {\n        return switch (ch) {\n            case 'R' -> 0x1;\n            case 'G' -> 0x2;\n            case 'B' -> 0x3;\n            case 'W' -> 0x4;\n            case 'Y' -> 0x5;\n            case ' ' -> 0x0;\n            default  ->\n                throw new IllegalArgumentException(\"Invalid char: \" + ch);\n        };\n    }\n}\n","sourceCodeStart":142,"sourceCodeEnd":164,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/0400-0499/0488.Zuma Game/Solution.java#L142-L164","documentation":"In the Java solution for LeetCode 488 (Zuma Game), the board and hand strings are compressed into a compact numeric encoding: 'R'->0x1, 'G'->0x2, 'B'->0x3, 'W'->0x4, 'Y'->0x5, ' '->0x0. Any other character hits the default branch and throws IllegalArgumentException('Invalid char: ' + ch). It fires when input contains a character outside the five allowed colors plus space.","triggerScenarios":"Calling encode() with a board/hand string containing lowercase letters, digits, '\\n', or any character other than R G B W Y and space; passing an untrimmed string with a tab or trailing newline.","commonSituations":"Feeding test data from a file without trimming newlines; case differences ('r' instead of 'R'); adapting the solver to a variant with extra colors (e.g. 'P' purple) without extending the switch; copy-paste introducing invisible characters.","solutions":["Sanitize input before encoding: uppercase and strip whitespace/newlines","Extend the switch with any additional valid characters your variant uses","Fail fast with a clear message including the offending char (already done) and fix the data source","Add a regex pre-check like input.matches(\"[RGBYW ]*\") to validate before calling encode"],"exampleFix":"// before\nencode(\"RGB\\n\"); // throws Invalid char: \n\n// after\nencode(\"RGB\\n\".trim()); // ok\n// or pre-validate: s.matches(\"[RGBYW ]*\")","handlingStrategy":"validation","validationCode":"if (!ch.matches(\"[RGBYW ]]*\".replace(\"]*\", \"]*\"))) throw new IllegalArgumentException(\"Invalid char: \" + ch); // pre-check\nboolean ok = s.chars().allMatch(c -> \"RGBYW \".indexOf(c) >= 0);","typeGuard":"private static boolean isValidChar(char ch) { return ch=='R'||ch=='G'||ch=='B'||ch=='W'||ch=='Y'||ch==' '; }","tryCatchPattern":"try { encode(s); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Invalid char:\")) { /* sanitize and retry */ } else throw e; }","preventionTips":["Trim and uppercase input before encoding","Validate with a whitelist regex","Extend the switch for new colors"],"tags":["java","validation","illegal-argument","encoding","leetcode"],"backgroundTag":"invalid-input-character","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}