Palindrome check
A puzzle feature checks whether a phrase reads the same backwards.
- Compare letters and digits only — skip spaces and punctuation.
- Case does not matter.
- An empty string counts as a palindrome.
isPalindrome(text: string) → bool
Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
boolean isPalindrome(String text) {
}
Worked examples
| Call | Result |
|---|---|
isPalindrome("") | true |
isPalindrome("racecar") | true |
isPalindrome("A man, a plan, a canal: Panama") | true |
isPalindrome("hello") | false |
Hint
Walk one index from each end, skipping anything that is not alphanumeric.
Reference solution in Java
boolean isPalindrome(String text) {
String s = text.toLowerCase().replaceAll("[^a-z0-9]", "");
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) return false;
}
return true;
}