A word-game app accepts a phrase as a palindrome if it reads the same forwards and backwards after ignoring case and every character that is not a letter or a digit. Read one line and print true or false.
Solve it in Java with two indices and the Character helper methods, without building a cleaned copy of the string.
Constraints
- 1 ≤ length ≤ 100000
Input and output
- Input
- One line of printable ASCII text (it may contain spaces).
- Output
- `true` or `false`.
Examples
Input 1
Never odd or even!Output
trueLetters only: neveroddoreven, which reads the same both ways.
Input 2
Race a carOutput
false"raceacar" reversed is "racaecar".
Show hints
- Move a left index forward and a right index backward, skipping characters that are not letters or digits.
- Compare with Character.toLowerCase.
Show the explanation
Use two pointers. Advance i past anything for which Character.isLetterOrDigit is false, move j back the same way, then compare Character.toLowerCase of both. Any mismatch means false. This uses O(1) extra space; building a filtered, lower-cased copy and reversing it is simpler but uses O(n) memory. A line with no letters or digits counts as a palindrome.
Original UniCircle question. Last reviewed 22 Oct 2026.
